Generating random number rand function

  • 2020-04-02 00:55:42
  • OfStack

function Rand () is the true random number generator , while srand() sets a random number seed for rand() to use. If you don't call srand() before the first call to rand(), the system will automatically call srand() for you. Calling srand() with the same number as the seed causes the same sequence of random Numbers to be generated.
Srand ((unsigned)time(NULL)) USES the value of the system timing/counter as a random seed. Seeds each corresponding to a set of according to the algorithm generated random number in advance, so that the same platform environment, different time to generate random Numbers will be different, accordingly, if the srand (unsigned) time (NULL) into srand (TP) (TP for any constant), no matter when to run, run, how many times have been the "random" would be a fixed set of sequences, so the srand generated random number is a pseudo-random number.
The library functions provide two functions for generating random Numbers: srand() and rand(). The prototype is:
Int rand(void);
Starting with the seed specified in srand (seed), returns a random integer between [0, RAND_MAX (0x7fff)].
Function 2: void srand(unsigned seed);
The parameter seed is the seed of rand(), which initializes the starting value of rand().
However, it is important to note that pseudorandom Numbers do not refer to false random Numbers. In fact, the absolute random number is just an ideal state of the random number, the computer can only generate relative random number is pseudorandom number. The pseudo-random Numbers generated by computers are both random and regular -- some obey certain rules, and some don't. For example, "there are no two leaves of the same shape", which points to the characteristic of things -- difference; But the leaves of every tree have a similar shape, and this is what things have in common -- regularity. From this point of view, we can accept the fact that computers can only produce pseudorandom Numbers and not absolute random Numbers.

The system will automatically call srand() before calling rand(), and if the user has called srand() before rand() and assigned a value to the parameter seed, rand() will use the seed value as the initial value to generate pseudo-random Numbers. If the user did not call srand() before rand(), the system defaults to 1 as the initial value of the pseudo-random number. If a fixed value is given, the sequence of random Numbers generated by rand() is the same every time ~~

So to avoid this we usually use srand((unsigned)time(0)) or srand((unsigned)time(NULL)) to generate seeds. If the interval is still too small, you can multiply (unsigned)time(0) or (unsigned)time(NULL) by some appropriate integer. For example, srand ((unsigned) time (NULL) * 10)
In addition, regarding time_t time(0) : time_t is defined as a long integer, which returns the time in seconds elapsed since January 1, 1970, zero minutes, zero seconds.
Generate the random number function rand usage, as shown in the code:

#include "stdafx.h"
#include <time.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
 //Initializes a random number seed
 //The time function returns the elapsed time in seconds from January 1, 1970, to zero minutes and zero seconds
 srand((int)time(NULL));
 int j;
 for (int i = 0; i < 10; i++) {
  j = (rand() * 10) / RAND_MAX + 1; //Generate random Numbers between 1 and 10
  printf("j = %d n", j);
 }
 unsigned start = (rand() * 1000)/ RAND_MAX + 15550; //Generate random Numbers between 15550 and 16549
 printf("start = %d n", start);
    start &= ~1; //Make start an even number, and if it's odd, make start an even number of start minus 1
 printf("start = %d n", start);
 getchar();
 return 0;
}

The results are as follows:
J = 9
J = 6
J = 7
J = 8
J = 1
J = 5
J = 3
J = 1
J = 10
J = 9
Start = 16185
Start = 16184

Related articles: