_c# tutorial on generating pseudo-random number in C # loop

Source: Internet
Author: User
Tags static class
The reason for this result is that the default seed for the Random () function is time, but when a random number is generated in the loop, the time used for the seed is the same (millisecond level) because the operation is too fast, so the sequence of random numbers is the same, so that the final random number will be the same. (Random number generator based on "linear Same congruential")
The workaround is to produce a globally unique identifier that uses its hash value to produce a random number of seeds. The code is as follows:
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Security.Cryptography;
Namespace calculation
{
public static Class Randomstatic
{
Random decimal number that produces [0,1]
public static double Producedblrandom ()
{
Random r = new Random (Guid.NewGuid (). GetHashCode ());//Use hash value of GUID to make seed
return r.nextdouble ();
}
Generate random integers within a set range
public static int Produceintrandom (int minvalue, int maxValue)
{
Random r = new Random (Guid.NewGuid (). GetHashCode ());
Return R.next (MinValue, MaxValue + 1);
}
}
}

This creates a random number in the loop that basically guarantees its randomness, and the code that uses the static class is as follows:
Copy Code code as follows:

Use the above static class
private void Button1_click_1 (object sender, EventArgs e)
{
for (int i = 0; i < i++)
{
Console.WriteLine (Randomstatic.produceintrandom (0,100));
}
}
Use default time Seed
private void Button2_click_1 (object sender, EventArgs e)
{
Random r = new Random ();
for (int i = 0; i < i++)
{
Console.WriteLine (R.next (0,100));
}
}

The first loop in the code above can produce 100 very good random numbers at 0-100, the second cycle, due to the use of the default time seed, in this cycle, the resulting sequence of random numbers is obviously relevant, can be two cycles of random number of sequences in Excel, to make a scatter plot, Results at a glance. Originally wanted to upload pictures, but csdn seems to upload the picture now.
The 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
The 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.