linux c random function rand srand introduction

1. When using rand () to generate random numbers, it generates 0 ~ RAND_MAX (the value depends on the platform, at least 32767, the platform I tested below is 0x7fffffff), but the random number generated by it Time pseudo-random number, the default random number seed is 1, so every time you re-run the program, the same random number will be generated. If the random number generated each time is different, you can use the current time time (0) as the random number seed .

 

2. The srand (seed) function is used to seed the rand () function.

 

3. Time is a function of the C language to obtain the current system time, in seconds, representing the number of seconds that the current time has passed since the Unix standard timestamp (0: 01: 00: 00 on January 1, 1970, GMT).

 

4. The unit of time generated by time (0) is seconds, so if you want to generate multiple random numbers within one second, then you should not use time (0) as a seed. Time (0) is generally only used for re-run It is necessary to generate different random numbers, otherwise the random numbers generated within this second will be the same. as follows:

Test code:


  
  
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. void main( void )
  5. {
  6. int i = RAND_MAX;
  7. printf( "RAND_MAX=0x%x time(0)=%d \n", RAND_MAX, ( int)time( 0));
  8. / * Output 10 random numbers. * /
  9. for (i = 0; i < 10; i++)
  10. {
  11. srand(time( 0));
  12. printf( "%6d ", rand());
  13. }
  14. printf( "\n");
  15. }

 

 

5. If srand (seed) has not been called before, it will automatically call srand (1) once, and you will find that it will output the same value every time it is re-run.

Test code:


  
  
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. void main( void )
  5. {
  6. int i = RAND_MAX;
  7. printf( "RAND_MAX=0x%x time(0)=%d \n", RAND_MAX, ( int)time( 0));
  8. / * Output 10 random numbers. * /
  9. for (i = 0; i < 10; i++)
  10. {
  11. printf( "%6d ", rand());
  12. }
  13. printf( "\n");
  14. }

 

 

Six, srand (seed) is used to set the seed for the rand () function, because the time will pass, the seed will change, and the random value obtained will be different.

Test code


  
  
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. void main( void )
  5. {
  6. int i = RAND_MAX;
  7. printf( "RAND_MAX=0x%x time(0)=%d \n", RAND_MAX, ( int)time( 0));
  8. srand(( int)time( NULL));
  9. / * Output 10 random numbers. * /
  10. for (i = 0; i < 10; i++)
  11. {
  12. printf( "%6d ", rand());
  13. }
  14. printf( "\n");
  15. }

 

 

7. To obtain a random integer between [a, b), use (rand ()% (ba)) + a (the result value will include a but not b). To obtain a random integer between [a, b], use (rand ()% (b-a + 1)) + a (the result value will include a and b).

Test code


  
  
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #define random_1(a, b) ((rand() % (b - a)) + a)
  5. #define random_2(a, b) ((rand() % (b - a + 1)) + a)
  6. void main( void )
  7. {
  8. int i = RAND_MAX;
  9. printf( "RAND_MAX=0x%x time(0)=%d \n", RAND_MAX, ( int)time( 0));
  10. srand(( int)time( NULL));
  11. / * Output 10 random numbers. * /
  12. printf( "[0,100): ");
  13. for (i = 0; i < 10; i++)
  14. {
  15. printf( "%6d ", random_1( 0, 100));
  16. }
  17. printf( "\n");
  18. printf( "[0,100]: ");
  19. for (i = 0 ; i < 10 ; i++)
  20. {
  21. printf( "%6d ", random_2( 0, 100));
  22. }
  23. printf ( "\n" );
  24. }

 

 

8. Use rand () / double (RAND_MAX) to obtain floating point numbers between 0 and 1.

Test code


  
  
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. void main( void )
  5. {
  6. int i = RAND_MAX;
  7. double ran_float = 0.0;
  8. printf( "RAND_MAX=0x%x time(0)=%d \n", RAND_MAX, ( int)time( 0));
  9. srand(( int)time( NULL));
  10. / * Output 10 random numbers. * /
  11. for (i = 0; i < 10; i++)
  12. {
  13. ran_float = rand() / ( double)(RAND_MAX);
  14. printf( "%6f ", ran_float );
  15. }
  16. printf( "\n");
  17. }

 

 

 

 

 

 

 

Published 35 original articles · Like1 · Visits 1870

1. When using rand () to generate random numbers, it generates 0 ~ RAND_MAX (the value depends on the platform, at least 32767, the platform I tested below is 0x7fffffff), but the random number generated by it Time pseudo-random number, the default random number seed is 1, so every time you re-run the program, the same random number will be generated. If the random number generated each time is different, you can use the current time time (0) as the random number seed .

Guess you like

Origin blog.csdn.net/lzj_linux188/article/details/105195606