The basic method of generating random numbers
In this paper, the author will introduce the use of random number generators provided by C language. The C compiler now provides a pseudo random number generator function based on an ANSI standard, which is used to generate random numbers. Both Microsoft and Borland support this standard through the rand () and Srand () functions, which work as follows:
First, the Srand () is provided with a "seed", which is a unsignde int type with a value range from 0 to 65,535;
Then, call Rand (), which returns a random number (between 0 and 32,767) based on the "seed" value provided to Srand ();
Call rand () multiple times as needed to get new random numbers continuously;
At any time, you can provide a new "seed" to Srand () to further "Randomize" the output of rand ().
The problem is that if you provide the same "seed" value every time you call Srand (), you will get the same "random" sequence of numbers. For example, after calling Srand () with 17 as the "seed" value, you will get a random number of 94 when you first call rand (), and you will get 26,602 and 30,017, respectively, when you call rand (for the second and third time). These numbers look fairly random (although this is just a small set of data points), however, after you invoke Srand () again with 17 as the "seed" value, the resulting return value is still 94, 26,602, and 30 in the first three calls to Rand (). 017, and the resulting return value is still the remaining return value obtained in the first batch of calls to Rand (). Therefore, only the Srand () is given a random "seed" value again to get a random number again.
The following example uses a simple and effective method to produce a fairly random "seed" value-the time value of the day.
# include <stdlib. H>
# include <stdio. H>
# include <sys/types. H>
# include <SYS/TIMEB. h>
void Main (void) {
int i;
unsigned int seedval;
Struct_timeb Timebuf;
_ftime (&TIMEBUF);
Seedval = (((unsigned int) timebuf, time & 0xFFFF) +
(unsigned int) timebuf, millitm) ^
(unsigned int) ti Mebuf, MILLITM);
Srand ((unsigned int) seedval);
for (i=o;i<lo;++i)
printf ("%6d\n", Rand ());
}
The previous example first invokes _ftime () to retrieve the current time and puts its value in the struct member Timebuf.time, the value of the current time is calculated in seconds starting January 1, 1970. After invoking _ftime (), the member Millitm in the struct timebuf also deposits the number of milliseconds that have elapsed in the current second, but in DOS this number is actually computed in 1% seconds. Then, add the number of milliseconds and seconds, and then the number of milliseconds to do a different or operation. You can exert more logical operations on the members of these two structures to control the range of Seedval and further enhance its randomness, but the logical operation used in the example above is sufficient.
Note that in the previous example, the output of rand () is not limited to a specified range, assuming you want to create a lottery selector that ranges from 1 to 44. You can simply ignore the value of rand () that is out of that range, but it will take a lot of time to get all the lottery numbers you need (for example, 6). Let's say you've built a random number generator that satisfies you, it produces a random range of data ranging from 0 to 32,767 (as mentioned in the previous article), and you want to limit the output to 1 to 44, and the following example shows how to do this:
int I, k, range;
int rain, max;
Double J;
Min=1; /* 1 is the minimum number allowed *
/max=44; /* is the maximum number allowed * * *
range=max-min; /* R is the range allowed; 1 to
/I=rand (); /* Use the above example into this slot/
* Normalize the rand () output (scale to 0 to 1) *///
* Rand_max is Defi Ned in Stdlib, H */
j= (Double) i/(double) rand_max);
/* Scale the output to 1 * *
* i= (int) (J * (double) range);
i+ =min;
The above example limits the random number of outputs to between 1 and 44, and works as follows:
Get a random number between O to Rand_max (32,767), dividing it by Rand_max, resulting in a correction value between 0 and 1;
Multiply the correction value by the desired range (in this case 43, or 44 minus 1), resulting in a value between O and 43;
Add the value to the minimum value required so that the value eventually falls within the correct range of values--1 to 44.
You can verify this example with different min and Max values, and you'll find that it will always correctly generate random numbers between the new rain and Max values.
Let's take a look at the random number of related exercise topics
Topic
Given the Rand7, how do I generate rand3?
Ideas
A very intuitive idea is to keep calling Rand7 until it produces a number between 1-3, and then returns. The code is as follows: (if a classmate said there is no 3, but does not mean that I can not judge the size of 3 compared with it)
#include <stdio.h>
int rand_3 ()
{
int x;
while (x = Rand_7 ()) {
if (x <= 3) {return
x;
}
}
}
Next, is to determine whether rand_3 can be equal to the probability of generating 1,2,3. That is, we need to calculate whether the probability of generating 1,2,3 is 1/3.
First of all, rand_7 can generate 1-7 of equal probabilities, and we take rand_3 1 as an example, assuming that:
- The first time you generate 1 is 1/7.
- The probability of a second survival 1 is 4/7 * 1/7, so the first time it must be a number greater than 3, such as 4,5,6,7, the probability is 4/7
- Similarly, the probability of a third generation of 1 is (4/7) ^2 * 1/7
Therefore, the probability that Rand_3 generates 1 is P (x=1) = 1/7 + (4/7) * 1/7 + (4/7) ^2 * 1/7 + ... + (4/7) ^n-1 * 1/7//Geometric progression
= 1/7 * ((1-(4/7) ^n)/1-4/7) = 1/7 * 7/3 = 1/3
Similarly, you can verify that the probability of generating 2, 3 is 1/3.
Conclusion
The above proof shows that RAND3 can produce 1,2,3 of equal probability. From the above analysis, we can draw a more general conclusion:
If a>b, we can certainly use rand_a to achieve rand_b. of which, rand_a is such a possible generation 1-a,rand_b is such a possible generation 1-b
Extended
Now given two random numbers of functions rand_a and rand_b,rand_a and rand_b respectively generate 1-a and 1-b random numbers, A and B are not equal, now let you use rand_a to implement Rand_b, the following method:
- If a>b, you can use the above method directly
- If a<b, the rand_a^2=a * (rand_a-1) + rand_a is constructed, representing the random number of the generated 1-a^2, and if the a^2 is less than B, proceed with the construction of Rand_a^3=a * (rand_a^2-1) + rand_a
Give an example to explain
Ali 2014 written question, is given to generate 1-7 of the immediately function rand_7, see whether can generate other random numbers?
Let's take a look at whether we can generate 1-49 for equal probability, construct rand_49 = 7 * (rand_7-1) + rand_7 PS: Don't ask me where 7 comes from, rand_7 since I can generate 1-7, of course I can get 7.
Rand_7-1 can generate equal probability of 0, 1, 2, 3, 4, 5, 6, the generation probability of each number is 1/7, so after *7, can be equal probability of generation 0,7,14,21,28,35,42, the probability of each number is 1/7
Since the probability of each number of 0,7,14,21,28,35,42 is 1/7, when each number is added +rand_7, then 1-49 is the equal probability, 1/7X1/7 = 1/49, the middle does not appear duplicate data
So, we use rand_7 to produce rand_49, with rand_49, we can, of course, get any random function less than 49 by the way it was originally filtered.