Function

**rand () is the real random number generator**, and Srand () sets the random number of seeds for use by rand (). If you do not invoke Srand () before the first call to Rand (), the system automatically invokes Srand () for you. Calling Srand () with the same number of identical numbers causes the same sequence of random numbers to be generated.

Srand (unsigned) time (NULL) uses the value of the system Timer/counter as a random seed. Each seed corresponds to a set of random numbers that are generated by the algorithm, therefore, in the same platform environment, the random number generated at different times will be different, accordingly, if you change the Srand (unsigned) time (NULL) to Srand (TP) (TP is any constant), then whenever you run, The number of "random numbers" that can be run is a set of fixed sequences, so the random numbers generated by Srand are pseudo-random numbers.

In a library function, the system provides two functions for generating random numbers: Srand () and Rand (). The prototype is:

**function One: int rand (void);**
Starts with the seed specified in Srand (seed) and returns a random integer between [0, Rand_max (0X7FFF)].

**function Two: void Srand (unsigned seed);**
**The ****parameter seed is the seed of rand () to initialize the starting value of rand (). **
However, it should be noted that the so-called "pseudo-random number" refers to not false random numbers. In fact, the absolute random number is only an ideal state of the random number, the computer can only generate a relative random number that is pseudo random number. The pseudo random number generated by computer is both random and regular--a part that follows certain laws, and one part does not obey any laws. For example, "There are no two identical leaves in the world," this is the nature of things--differences, but the leaves of each tree have an approximate shape, which is the commonness of things--regularity. From this point of view, we can accept the fact that a computer can only produce pseudorandom numbers instead of absolute random numbers.

The system will automatically invoke Srand () before calling Rand (). If a user has called Srand () before Rand () to specify a value for the parameter seed, then rand () takes the seed value as the initial value for generating the pseudo-random number, and if the user is in rand () Srand () was not previously invoked, 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 produced by Rand () is the same every time.

So in order to avoid this, we usually use Srand ((unsigned) time (0)) or Srand ((unsigned) time (NULL)) to produce the seed. If you still feel that the interval is too small, you can multiply a suitable integer after (unsigned) time (0) or (unsigned) times (NULL). For example, Srand ((unsigned) time (NULL) *10)

In addition, about time_t time (0): time_t is defined as a long integer, which returns the period, in seconds, that has elapsed since January 1, 1970, 0 minutes, 0 seconds, and so far.

Generate the random number function Rand usage, as shown in the code:

__Copy Code__ code as follows:

#include "stdafx.h"

#include <time.h>

#include <stdlib.h>

int _tmain (int argc, _tchar* argv[])

{

Initialize random number of seeds

The time function returns the duration of 0 minutes, 0 seconds from January 1, 1970, in seconds

Srand ((int) time (NULL));

Int J;

for (int i = 0; i < i++) {

j = (rand () *)/Rand_max + 1; To generate random numbers between 1~10

printf ("j =%d \ n", j);

}

unsigned start = (rand () * 1000)/Rand_max + 15550; To generate random numbers between 15550~16549

printf ("start =%d \ n", start);

Start &= ~; Turn start into an even number, if it is odd, then start becomes an even number of start-1

printf ("start =%d \ n", start);

GetChar ();

return 0;

}

the results of the run 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