※C ++ essay ※=) ☆c ++ basics ☆=) ※rand random sequence function in sequence → C ++

Source: Internet
Author: User

Standard Library (Included in ) Provides two functions to help generate pseudo-random numbers:
Function 1: int rand (void );
Returns a random integer between [seed, RAND_MAX (0x7fff) starting from the seed specified in srand (seed.
Function 2: void srand (unsigned seed );

The seed parameter is the seed of rand () and used to initialize the starting value of rand. (Generally, we use the time (NULL) seed, from a time point (usually January 1, 1970 00:00:00) to the number of seconds (that is, the calendar time ))

# Define RAND_MAX 0x7fffu
This is the definition in bcc55, indicating that the maximum number of this integer is 0x7fffu, and u represents unicode encoding.

PS: the return value of time (NULL) is time_t, which is actually a long integer, to a certain day in the future. For a value of the time_t data type, the time it represents cannot be later than January 18, 2038. However, it is basically enough for everyone to use. If it takes a longer time, you can use 64 _ time64 for processing.


When rand () is called, it will view:

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, this variable value must be different each time the program runs (for example, the time 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_MAX (0x7fff )) between a random value.
3) If srand (seed) is not called before calling rand (), the effect will be the same as calling rand (1) and then calling rand () (1 is also a set value ).

Usage of Random Number Generation
1) provide a seed for srand (), which is an unsigned int type;
2) call rand (), which returns a random number (between 0 and RAND_MAX) based on the seed value provided to srand );
3) call rand () multiple times as needed to continuously obtain new random numbers;
4) at any time, srand () can be provided with a new seed to further "randomize" the output results of rand.

General expression formula for random numbers within a certain range

To obtain a random integer of [a, B), use (rand () % (B-a) +; To obtain a random integer of [a, B], use (rand () % (B-a + 1) +; To obtain a random INTEGER (a, B], use (rand () % (B-a) + a + 1; General Formula: a + rand () % n; where a is the starting value and n is the Integer Range. To obtain a random integer between a and B, a + (int) B * rand ()/(RAND_MAX + 1 ). 0 ~ The floating point between 1. You can use rand ()/double (RAND_MAX ).


MSDN standard sample code

// crt_rand.c// This program seeds the random-number generator// with the time, then exercises the rand function.//#include 
 
  #include 
  
   #include 
   
    void SimpleRandDemo( int n ){   // Print n random numbers.   int i;   for( i = 0; i < n; i++ )      printf( "  %6d\n", rand() );}void RangedRandDemo( int range_min, int range_max, int n ){   // Generate random numbers in the half-closed interval   // [range_min, range_max). In other words,   // range_min <= random number < range_max   int i;   for ( i = 0; i < n; i++ )   {      int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)            + range_min;      printf( "  %6d\n", u);   }}int main( void ){   // Seed the random-number generator with the current time so that   // the numbers will be different every time we run.   srand( (unsigned)time( NULL ) );   SimpleRandDemo( 10 );   printf("\n");   RangedRandDemo( -100, 100, 10 );}
   
  
 


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