# Usage of C random functions rand () and srand ()

Source: Internet
Author: User

Rand and srand usage
First, we need to have a general opinion on rand & srand: srand initializes random seeds, and rand generates random numbers. The following describes in detail.
Rand (Random Number Generation)
Define function: int rand (void)
Function Description:
Because the internal implementation of rand is made by the linear same remainder method, it is not a real random number, but because its cycle is very long, so it can be considered as random within a certain range, rand () returns a random value ranging from 0 to RAND_MAX. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1. Rand () generates false random numbers, which are the same during each execution. To be different, initialize it with different values. The initialized function is srand ().
Return Value:
Returns a random integer between 0 and RAND_MAX. The range of RAND_MAX is at least 32767 (int), that is, double byte (16 digits ). If unsigned int is used, the dual-byte value is 65535, and the four-byte value is an integer range of 4294967295.
0 ~ RAND_MAX the probability of each number being selected is the same.
Example:
/* Generate a random value ranging from 1 to 10. In this example, no random seed is set. For the complete random number generation, see
Srand ()*/
# Include
Main ()
{
Int I, j;
For (I = 0; I <10; I)
{
J = 1 (int) (10.0 * rand ()/(RAND_MAX 1.0 ));
Printf ("% d", j );
}
}
Run:
9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6 // re-execution still produces the same random number
Srand (set Random Seed)
Define the function: void srand (unsigned int seed );
Function Description:
Srand () is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of geypid () or time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.
Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with rand */
# Include
# Include
Main ()
{
Int I, j;
Srand (int) time (0 ));
For (I = 0; I <10; I)
{
J = 1 (int) (10.0 * rand ()/(RAND_MAX 1.0 ));
Printf ("% d", j );
}
}
Execution: Compare with the rand example
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
Or:
Use "int x = rand () % 100;" to generate a random number between 0 and 100. This method is not or can be used. A better method is j = (int) (n * rand ()/(RAND_MAX 1.0) generates a random number between 0 and n.
Int main (void)
{
Int I;
Time_t t;
Srand (unsigned) time (& t ));
Printf ("Ten random numbers from 0 to 99 ");
For (I = 0; I <10; I)
Printf ("% d", rand () % 100 );
Return 0;
}
In addition to the preceding descriptions, the srand function must be placed out of the loop or out of the loop. Otherwise, the same number is obtained.
The example in MSDN.
// Crt_rand.c
// This program seeds the random-number generator
// With the time, then displays 10 random integers.
//
# Include
# Include
# Include
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 ("m", rand ());
Printf ("");
// Usually, you will want to generate a number in a specific range,
// Such as 0 to 100, like this:
{
Int RANGE_MIN = 0;
Int RANGE_MAX = 100;
For (I = 0; I <10; I)
{
Int rand100 = (double) rand ()/
(Double) RAND_MAX) * RANGE_MAX RANGE_MIN );
Printf ("m", rand100 );
}
}
Summary:
We know that the rand () function can be used to generate random numbers, but this is not a real random number, it is a pseudo random number, it is based on a number, we can call it a kind, A coefficient calculated based on a recursive formula. When the number of series is large, it is in line with the normal announcement, which is equivalent to generating a random number, but this is not a real random number, after the computer starts up normally, the seed value is fixed. Unless you break the system, C provides the srand () function to change the seed value, its prototype is void srand (int a). The function is
Initialize the initial values of the random generator rand () function, even if the seed value is changed to a. From this you can see that through the sand () function, we can generate foreseeable random sequences,
Then how can we generate unforeseen random sequences? We may often need such a random sequence, right. Srand (unsign) (time (NULL) is a method, because the time of each running program is different, right, You know time () the function returns the number of seconds from to the present. Maybe the start time is incorrect. check whether it is correct. C also provides another more convenient function, randomize ()
The original form is void randomize (), which is used to start the initial value of the rand () seed, and the value is uncertain. It is equivalent to srand (unsign) (time (NULL )) however, it should be noted that the randomize () function should be implemented through time, so the header file should contain time when calling it. h.

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.