Computer random numbers are all pseudo-random numbers, that is, they are generated by a small M polynomial sequence. Each generated small sequence has an initial value, namely the random seed. (Note: The period of the small M polynomial sequence is 65535, that is, the period of the random numbers generated each time using a random seed is 65535. When you obtain 65535 random numbers, they repeat again.)
We know that the rand() function can be used to generate random numbers, but this is not a random number in the true sense; it is a pseudo-random number. It is a series of numbers calculated based on a number (we can call it a seed) using a certain recurrence formula. When this series of numbers is very large, it conforms to a normal distribution, thus equivalent to generating random numbers, but it is not a true random number. When the computer boots normally, the value of this seed is fixed, unless you break the system.
1.rand()
Function:
Random number generator
Usage:
int rand(void)
Header file:
stdlib.h
The internal implementation of rand() uses the linear congruential method. It is not a true random number, but because its period is particularly long, it can be regarded as random within a certain range.
rand() returns a random value in the range 0 to RAND_MAX. The range of RAND_MAX is at least 32767 (int). With unsigned int, it is 65535 for two bytes, and the integer range is 4294967295 for four bytes. Each number from 0 to RAND_MAX has the same probability of being selected.
When the user does not set the random number seed, the system default random number seed is 1.
rand() produces pseudo-random numbers, which are the same each time it is executed; to make them different, use the function srand() to initialize it.
2.srand()
Function:
Initialize the random number generator
Usage:
void srand(unsigned int seed)
Header file:
stdlib.h
srand() is used to set the random number seed when rand() generates random numbers. The parameter seed must be an integer. If seed is set to the same value each time, the random number generated by rand() will be the same each time.
3. Use the current clock as the random number seed
The random numbers generated by rand() are the same as the previous time every time it runs. To make them different, use the function srand() to initialize it. You can use the method srand((unsigned int)(time(NULL)) to generate different random number seeds, because the time of each program run is different.
4. Usage for generating random numbers
- 1) Provide a seed to srand(), which is of type unsigned int;
- 2) Call rand(), which will return a random number (between 0 and RAND_MAX) based on the seed value provided to srand();
- 3) Call rand() multiple times as needed, thereby continuously getting new random numbers;
- 4) At any time, you can provide a new seed to srand(), thereby further "randomizing" the output of rand().
Program for random numbers between 0 and RAND_MAX
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
for(int i = 0; i < 10;i++ )
cout << rand() << '/t';
cout << endl;
return 0;
}
5. General formula for generating random numbers in a certain range
To obtain a random integer in [a,b), use (rand() % (b-a)) + a;
To obtain a random integer in [a,b], use (rand() % (b-a+1)) + a;
To obtain a random integer in (a,b], use (rand() % (b-a)) + a + 1;
General formula: a + rand() % n; where a is the starting value and n is the range of integers.
To obtain a random integer between a and b, another representation: a + (int)b * rand() / (RAND_MAX + 1).
To obtain a floating-point number between 0 and 1, you can use rand() / double(RAND_MAX).
Original link: https://blog.csdn.net/hgl868/article/details/7057843