C++中的随机数

1. rand() function

 

  • header file : cstdlib
  • to obtain a random integer between 0 and 9, use rand() % 10
  • In fact, the numbers produced by rand() are pseudorandom, i.e. it produces the same sequence of numbers every time it is executed on the same system, because the algorithm used by the rand() function uses a value called the seed to control how to generate the numbers. By default the seed value is 1. 
2. srand(seed) function 
  • header file : cstdlib
  • function : to change the seed 
  • srand(time(0)) : to ensure that the seed value is different each time you run the program

 

你可能感兴趣的:(C++,Random)