我正在尝试编写一个使用种子生成伪随机数的程序。然而,我遇到了一些问题。
我得到了这个错误
39 C:\Dev-Cpp\srand_prg.cpp void value not ignored as it ought to be 使用以下代码
#include <iostream>
#include <iomanip>
#include <sstream>
#include <limits>
#include <stdio.h>
using namespace std ;
int main(){
int rand_int;
string close ;
close == "y" ;
cout << endl << endl ;
cout << "\t ___________________________________" << endl ;
cout << "\t| |" << endl ;
cout << "\t| Pseudorandom Number Game! |" << endl ;
cout << "\t|___________________________________|" << endl ;
cout << endl << endl ;
while ( close != "y" ){
rand_int = srand(9);
cout << rand_int << endl ;
cout << " Do you wish to exit the program? [y/n] " ;
cin >> close ; }
}发布于 2010-11-12 12:01:35
srand不会返回随机数,它只是重新设定随机数生成器的种子。之后调用rand来实际获取一个数字:
srand(9);
rand_int = rand();发布于 2010-11-12 12:01:58
srand()生成一个种子(用于初始化随机数生成器的数字),并且必须在每个进程中调用一次。rand()是您要查找的函数。
如果您不知道要选择哪个种子,请使用当前时间:
srand(static_cast<unsigned int>(time(0))); // include <ctime> to use time()发布于 2010-11-12 12:01:53
这么说吧。
srand(9);
rand_int = rand();https://stackoverflow.com/questions/4161501
复制相似问题