C++中产生随机数的基础是什么?
这背后有什么逻辑或原则吗?
生成的数字是完全随机的吗?
假设我正在运行这个程序:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main()
{
/*
Declare variable to hold seconds on clock.
*/
time_t seconds;
/*
Get value from system clock and
place in seconds variable.
*/
time(&seconds);
/*
Convert seconds to a unsigned
integer.
*/
srand((unsigned int) seconds);
/*
Output random values.
*/
cout<< rand() << endl;
cout<< rand() << endl;
cout<< rand() << endl;
return 0;
}
它显示了什么:http: //img14.imageshack.us/img14/1538/98271820.png
它显示 205 两次。