Surely, everyone knows about the existence of .Net class System.Random, allowing supposedly random numbers to receive little or no special both mentally and time-consuming. Imagine that we have such a method that is purely for the sake of the experiment we call the cycle several times:
1
2
3
4
5
6
7
8
9
10
| <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > static string GetRandomNum( int minValue, int maxValue)</span> static string GetRandomNum ( int minValue, int maxValue)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >Random rnd = new Random();</span> Random rnd = new Random ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return rnd.Next(minValue, maxValue).ToString();</span> return rnd.Next (minValue, maxValue) .ToString ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > for ( int i = 0; i < 10; i++)</span> for ( int i = 0; i <10; i ++)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >Console.WriteLine(GetRandomNum(1,11));</span> Console.WriteLine (GetRandomNum (1,11));</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
In this case, you will get exactly the same all the "random" numbers.
There is so because when initializing Random object using the default constructor as the numbers used to calculate the random number used Environment.TickCount (the number of milliseconds since the start of the system), which is updated every 15.6 milliseconds.
Accordingly, at short intervals and receive the same "random" number. And by the way, if you turn on the computer every day at the same time, this code is at one and the same time from the beginning of the launch of the operating system in a few days, you too will receive the same awesome random values. And if within the same period, the on-off the computer this can be combated by creating and initializing an object of class Random once, so that the values are all pretty predictable within a few days in Random can not do anything.
For guaranteed a much more predictable and random numbers in .Net there is a class RNGCryptoServiceProvider, located in the namespace System.Security.Cryptography, in which we are interested in the method GetBytes (byte [] data), fills an array byte cryptographically random sequence of numbers. But what about using it to get what we have received from Random, namely, a random number is between two specified values?
Make it pretty easy, if you imagine the resulting random bytes as a number between 0 and 1, and make him simple math:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > static string GetRandomNum( int minValue, int maxValue)</span> static string GetRandomNum ( int minValue, int maxValue)</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >{</span> {</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >System.Security.Cryptography.RNGCryptoServiceProvider rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();</span> System.Security.Cryptography.RNGCryptoServiceProvider rnd = new System.Security.Cryptography.RNGCryptoServiceProvider ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //Получаем наш случайный байт</span> // Get our random bytes</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > byte [] randombyte = new byte [1];</span> byte [] randombyte = new byte [1];</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >rnd.GetBytes(randombyte);</span> rnd.GetBytes (randombyte);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //превращаем его в число от 0 до 1</span> // convert it into a number from 0 to 1</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > double random_multiplyer = (randombyte[0] / 255d);</span> double random_multiplyer = (randombyte [0] / 255d);</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //получаем разницу между минимальным и максимальным значением</span> // get the difference between the minimum and maximum values</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > int difference = maxValue-minValue+1;</span> int difference = maxValue-minValue + 1;</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > //прибавляем к минимальному значение число от 0 до difference</span> // add to the value of the minimum number of between 0 and difference</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > int result = ( int )(minValue + Math.Floor(random_multiplyer * difference));</span> int result = ( int ) (minValue + Math.Floor (random_multiplyer * difference));</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" > return result.ToString();</span> return result.ToString ();</span> <span class = "notranslate" onmouseover= "_tipon(this)" onmouseout= "_tipoff()" ><span class = "google-src-text" style= "direction: ltr; text-align: left" >}</span> }</span> |
The result of the code sequence number is already in the first, more diverse, and, secondly, it will be truly random.
0 коммент.:
Post a Comment