Srand () is to provide seed for rand ().
If srand inputs the same value each time, the random number generated each time is the same,
Srand (N)
For (10)
Rand ()
That is to say, using a fixed value as the seed is a disadvantage. The common practice is to replace this code srand (unsigned) Time (null);, which will make the seed a non-fixed number, this random number will not be the same for every execution.
1. Let's look at an example.
# Include <iostream>
# Include <stdlib. h>
# Include <time. h>
Using namespace STD;
Int main (void)
{
Int I;
/* Seed the random-number generator with current time so that
* The numbers will be different every time we run.
*/
Srand (unsigned) Time (null ));
/* Display 10 numbers .*/
For (I = 0; I <10; I ++)
Printf ("% 6D/N", Rand ());
}
2. About time. h
Time. h contains many interesting functions, such
Char * ctime (long * Clock)
This function converts the time specified by clock (such as the time returned by the function time)
String: Mon Nov 21 11:31:54, 1983/n/0
# I nclude <iostream>
# I nclude <stdlib. h>
# I nclude <time. h>
Using namespace STD;
Void main ()
{
Time_t T1, T2;
Char gettime [20];
Char * ptstring = gettime;
Int X, Count = 0;
X = rand_max;
Cout <'/N ';
T1 = Time (null );
Ptstring = ctime (& T1 );
While (count <= 100)
{
Srand (unsigned) Time (null ));
X = rand () % 50;
If (x <5)
Continue;
Else
{
Count ++;
Cout <"The numth is" <'/N ';
}
}
When you view the ptstring value, "Tue Sep 13 16:31:06 2005" is displayed"
3. Finally, let's talk about the srand () function.
Void srand (unsigned seed) initialize the random number generator
Some discussions are as follows:
1. The C function library does not directly put the important operations for initializing random seeds using the system clock into the ran
In the implementation of the D function, I think there are at least three reasons:
(1) continuous random numbers can be generated efficiently without every initialization;
(2) give programmers higher flexibility, because it is possible to use better data in scenarios with higher requirements.
Seed, not the system clock;
(3) initialization is not required to generate a large number of pseudo-random numbers for some verification or statistics.
Every time the program runs, it generates the same series of random numbers. In some cases, this does not matter.
In fact, there is a more important reason:
The rand () function of the pseudo-random sequence generator must have an important feature:
Columns must be reproducible.
This is not just an algorithm. To a large extent, it is related to the accuracy of code testing. If the algorithm
Using data related to the results of rand (), we have the opportunity to reproduce each
Test process to locate the problem more effectively.
So here is a suggestion. in the Code, if the result of the Rand () function is related to the result of the algorithm
Make sure that your rand () call is reproducible.
4. Usage of functions rand () and srand () in C language --
Rand (void) is used to generate a pseudo random unsigned int integer.
Srand (SEED) is used to set seeds for the rand () function.
Srand and Rand should be set and used. In general, srand is used to set the Rand.
For example:
Srand (uint) getcurrenttime ());
Int x = rand () %100;
Is to generate a random number between 0 and 100.
Srand () is used to initialize the random number of seeds, because the internal implementation of Rand is done by the linear same remainder method, which is not true.
The random number is only because its cycle is particularly long. Therefore, it can be regarded as random within a certain range. The formula is as follows:
:
Rand = Rand * const_1 + c_var;
The srand function is the First Rand value for it.
Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is optional,
A good practice is: J = (INT) (N * rand ()/(rand_max + 1.0) generate a random number between 0 and N.
Quantity
Rand_max = 0x7fffffff
5. Summary
1) srand () provides seeds for rand ()
2) seed in srand () is generally obtained by the time function. For example, srand (uint) getcurrenttime () srand (u
Nsigned) Time (null) Time () function to get the current system time...