srand()、C++ 的问题
我正在尝试编写一个使用种子生成伪随机数的程序。但是,我遇到了问题。
我收到此错误
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 ; }
}
I'm trying to write a program that generates a pseudorandom numbers using a seed. However, I'm running into problems.
I get this error
39 C:\Dev-Cpp\srand_prg.cpp void value not ignored as it ought to be
Using this code
#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 ; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
srand
不返回随机数,它只是重新播种随机数生成器。之后调用rand
来实际获取一个数字:srand
doesn't return a random number, it just reseeds the random number generator. Callrand
afterwards to actually get a number:srand() 生成一个种子(用于初始化随机数生成器的数字),并且每个进程必须调用一次。 rand() 是您正在寻找的函数。
如果您不知道要挑选什么种子,请使用当前时间:
srand() generates a seed (which is the number used to initialize the random number generator) and must be called once per process. rand() is the function you are looking for.
If you don't know what seed to pick, use the current time:
这样称呼它。
call it this way.
您错误地使用了
srand
,该特定函数用于为以后调用rand
设置种子。基本思想是使用不确定的种子调用
srand
一次,然后连续调用rand
以获得数字序列。类似于:它应该给你一些 0 到 9 之间的随机数(包括 0 和 9)。
通常,您不会将种子设置为特定值,除非您正在测试或出于其他原因需要相同的数字序列。您也不必每次在调用
rand
之前设置种子,因为您可能会重复获得相同的数字。所以你的特定
while
循环更像是:You're using
srand
incorrectly, that particular function is for setting the seed for later calls torand
.The basic idea is to call
srand
once with an indeterminate seed, then callrand
continuously to get a sequence of numbers. Something like:which should give you some random numbers between 0 and 9 inclusive.
You generally don't set the seed to a specific value unless you're testing or you want an identical sequence of numbers for some other reason. You also don't set the seed each time before you call
rand
since you're likely to get the same number repeatedly.So your particular
while
loop would be more like:srand 返回 void 函数并且不返回值。
在这里您可以看到更多相关信息。你只需要调用 srand(9) 并获取 rand() 的值,就像 J-16 SDiZ 指出的那样,谁会因此而投票:)
srand returns void function and doesn't return a value.
Here you can see more about it. You'll just have to call srand(9) and get the value of rand() after that, like J-16 SDiZ pointed out, who will be upvoted for this :)