gpt4 book ai didi

c++ - srand() 的问题,C++

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:36 25 4
gpt4 key购买 nike

我正在尝试编写一个使用种子生成伪随机数的程序。但是,我遇到了问题。

我收到这个错误

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 ; }

}

最佳答案

srand 不返回随机数,它只是重新播种随机数生成器。之后调用 rand 实际得到一个数字:

srand(9);
rand_int = rand();

关于c++ - srand() 的问题,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4161501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com