Rand and srand usage of Random Functions

Source: Internet
Author: User
Tags random seed

I. Basics

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 is properly started, the seed value is fixed unless you break the system.

To change the value of this seed, C provides the srand () function. Its prototype is void srand (int A) function, which is used to initialize the initial values of both the rand () function of the random generator, even if you change the seed value to a, you can see that through the sand () function, we can generate predictable random sequences.

Srand (SEED );
Then you can call Rand (). The random numbers generated by the same seed are arranged in the same way.
For example
Srand (20 );
Assume that the random number, is generated after rand () is called, and then the program exits. What is generated during the next running is ,.
Srand (Time (null) is used to generate different random numbers each time ));
Remember to use the header file stdlib. h for srand () and time. h for time (null ).

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,

The original form of randomize () is void randomize (). It is used to start the initial values of the seeds of rand (), 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. Therefore, when calling it, the header file should contain time. H.

Ii. Example

Srand (set Random Seed)
Related functions
Rand

# Include <stdlib. h>

Define functions
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.

Return Value

Example
/* Generate a random number ranging from 1 to 10. This example and execution result can be referenced with RAND */
# Include <time. h>
# Include <stdlib. h>
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 );
}
}

Run
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7

Rand (Random Number Generation)
Related functions
Srand

# Include <stdlib. h>

Define functions
Int rand (void)

Function Description
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. For details about random seed, see srand ().

Return Value
Returns a random value between 0 and rand_max. rand_max is defined in stdlib. h and its value is 2147483647.

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 <stdlib. h>
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

1. Rand returns a pseudo-random integer with a uniform distribution between 0 and rand_max. Rand_max must be 32767 at least. The rand () function does not accept parameters. The default value is 1 (the start value ). The random number generator always starts with the same seed, so the pseudo-random sequence is also the same, without the random meaning. (But this facilitates program debugging)
2. In C ++, another function srand () can specify different numbers (unsigned integer yuan) as seeds. However, if the seeds are the same, the pseudo-random sequence is also the same. One way is to let the user input seeds, but it is still not ideal.
3. It is ideal to use a variable number, such as time, as the seed of the random number generator. The value of time varies every moment. So the seeds are different, so the random numbers are also different.
// C ++ random function (VC Program)
# Include <stdlib. h>
# Include <iostream>
# Include <time. h>
Using namespace STD;
# Deprecision max 100
Int main (INT argc, char * argv [])
{
Srand (unsigned) Time (null); // The srand () function generates a Random Seed starting from the current time
For (INT I = 0; I <10; I ++)
Cout <rand () % max <Endl; // Max is the maximum value, and its random field is 0 ~ MAX-1
Return 0;
}

Related Keywords:

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.