C # generate a pseudo-random number in a loop

Source: Internet
Author: User

The reason for this result is that the default seed of the random () function is time, but when a random number is generated in a loop, the calculation speed is too fast, the time used for seeding is the same (in milliseconds), so the random number sequence generated is the same, so that the final random number will be the same. (Random number generator based on "linear same remainder method)
The solution is to generate a globally unique identifier and use its hash value as the seed to generate a random number. Code As follows: Copy code The Code is as follows: using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Security. cryptography;
Namespace Calculation
{
Public static class randomstatic
{
// Generate a random decimal number of [0, 1)
Public static double producedblrandom ()
{
Random r = new random (guid. newguid (). gethashcode (); // use the hash value of guid as the seed
Return R. nextdouble ();
}
// Generate a random integer within the specified range
Public static int produceintrandom (INT minvalue, int maxvalue)
{
Random r = new random (guid. newguid (). gethashcode ());
Return R. Next (minvalue, maxvalue + 1 );
}
}
}

In this way, the random number generated in the loop can basically ensure its randomness. The code for using this static class is as follows:Copy codeThe Code is as follows: // use the above static class
Private void button1_click_1 (Object sender, eventargs E)
{
For (INT I = 0; I <100; I ++)
{
Console. writeline (randomstatic. produceintrandom (0,100 ));
}
}
// Use the default time seed
Private void button2_click_1 (Object sender, eventargs E)
{
Random r = new random ();
For (INT I = 0; I <100; I ++)
{
Console. writeline (R. Next (0,100 ));
}
}

The first loop in the above Code can generate 100 good random numbers ranging from 0 to, while the second loop, because the default time seed is used, in this loop, the generated random number sequence is obviously correlated. You can place the random number sequence generated by two cycles in Excel to create a scatter chart. The result is clear at a glance. I wanted to upload images, but csdn does not seem to be able to upload images now.
Result of the first loop:
37
66
70
82
82
58
85
60
78
13
3
9
75
83
63
43
50
11
56
13
79
58
30
7
84
5
92
48
83
3
5
29
36
29
8
82
20
1
46
49
17
87
95
35
62
20
51
97
18
41
26
28
63
90
59
76
23
94
11
63
12
37
2
54
23
24
66
86
23
65
3
86
25
85
22
43
17
53
86
89
51
14
59
46
66
54
2
58
75
2
88
99
87
9
31
96
92
8
89
23
Result of the second loop:
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43
43

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.