Summary of the methods for generating pseudo-random numbers using the C language rand () and srand ()

Source: Internet
Author: User
Tags random seed

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.Srand ()Used to set the Random Seed when rand () is used to generate 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.

The standard library <cstdlib> is included in <iostream>) provides two functions to help generate pseudo-random numbers:

Function 1: int rand (void );

Returns a random integer between the specified seed in srand (seed) and [seed, RAND_MAX0x7fff.

Function 2: void srand (unsigned seed );

The seed parameter is the seed of rand () and used to initialize the starting value of rand.

It can be considered that rand () is viewed every time it is called:

1) If you have previously called srand (seed) and specified a value for seed, it will automatically call srand (seed) once to initialize its starting value.

2) If you have not called srand (seed) before, it will automatically call srand (1) once.

Based on the first point above, we can conclude that:

1) If you want rand () to generate different values each time the program runs, you must change the value of seed in srand (seed, the change value must be different during each program running, for example, the time that has elapsed so far ).

2) Otherwise, if you specify a value for seed, the value generated by rand () will be the same each time the program runs, although the value will be [seed, RAND_MAX0x7fff )) between a random value.

3) If srand (seed) is not called before calling rand (), the result will be the same as calling srand (1) and then calling rand (). 1 is also a set value ).

For example, we want to obtain 0 ~ The random integers between 6 do not include 6 itself ):

Example 1: do not specify seed:

  1. for(int i=0;i<10;i++)  
  2. {   
  3. ran_num=rand() % 6;  
  4. cout<<ran_num<<" ";  

Output for each operation: 5 5 4 4 5 4 0 4 2

Example 2: Specify seed as the value 1:

  1. srand(1);  
  2. for(int i=0;i<10;i++){   
  3. ran_num=rand() % 6;  
  4. cout<<ran_num<<" ";  

Output for each operation: 5 5 4 4 5 4 0 4 2

The result is exactly the same as that of Example 1.

Example 3: Specify seed as the value 6:

  1. srand(6);  
  2. for(int i=0;i<10;i++)  
  3. {   
  4. ran_num=rand() % 6;  
  5. cout<<ran_num<<" ";  

Output after each operation: 4 1 5 1 4 3 4 2 2

The random value is also between [0, 6). The random value is different from srand (1), but the result of each running is the same.

Example 4: Specify the seed as the unit of time in seconds when the current system is elapsed.): time_t time (0 ):

  1. #include <ctime>  
  2. //…  
  3. srand((unsigned)time(0));  
  4. for(int i=0;i<10;i++)  
  5. {   
  6. ran_num=rand() % 6;  
  7. cout<<ran_num<<" ";  

Output at first run: 0 1 5 4 5 0 2 3 4 2

Second: 3 2 3 0 3 5 5 2 2 3

In short, each running result will be different, because each time the program is started, the interval must be greater than 1 second ?, See ).

About time_t time (0 ):

Time_t is defined as a long integer. It returns the time elapsed since 00:00:00, January 1, January 1, 1970, in seconds. For example, assume that the output is:

  1. cout<<time(0); 

The value is about 1169174701, which is about 37 years) multiplied by 365 days) multiplied by 24 hours) multiplied by 3600 seconds ).

In addition, about ran_num = rand () % 6,

It is necessary to evaluate the modulo between the return value of rand () and 6, so that the random number of the target falls between [). Otherwise, the return value of rand () may be huge.
A general formula is:

To obtain a random integer between [a, B), use rand () % (B-a) + a to obtain a result value that does not contain B ).

When a is 0, it is abbreviated as rand () % B.

Finally, the pseudo-random Floating Point Number:

Use rand ()/double (RAND_MAX) to obtain 0 ~ Note that the floating point between 1 is different from the integer formula by dividing, not modulo). For example:

  1. double ran_numf=0.0;  
  2. srand((unsigned)time(0));  
  3. for(int i=0;i<10;i++)  
  4. {   
  5. ran_numf = rand() / (double)(RAND_MAX);  
  6. cout<<ran_numf<<" ";  

Running result: 0.716636, 0.457725 ,... And so on ~ The floating point number between 1 and the result is different each time.

If you want to get a larger range of random floating point numbers, such as 1 ~ 10.

  1. rand() /(double)(RAND_MAX)  


  1. rand() /(double)(RAND_MAX/10) 

Running result: 7.19362, 6.45775 ,... And so on ~ The floating point number between 10 and the result is different each time.

As for, and so on.

The above is not the best implementation method for pseudo-random floating-point numbers, but it can be used.

Contact Us

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: 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.