C++中的随机数_C/C++_编程开发_程序员俱乐部
C++中的随机数
- 摘要:1.rand()functionheaderfile:cstdlibtoobtainarandomintegerbetween0and9,userand()%10Infact,thenumbersproducedbyrand()arepseudorandom,i.e.itproducesthesamesequenceofnumberseverytimeitisexecutedonthesamesystem,becausethealgorithmusedbytherand(
- 标签: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
?