srand()、C++ 的问题

发布于 2024-10-01 17:25:26 字数 1005 浏览 5 评论 0原文

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

我收到此错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

凉风有信 2024-10-08 17:25:26

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

srand(9);
rand_int = rand();

srand doesn't return a random number, it just reseeds the random number generator. Call rand afterwards to actually get a number:

srand(9);
rand_int = rand();
各空 2024-10-08 17:25:26

srand() 生成一个种子(用于初始化随机数生成器的数字),并且每个进程必须调用一次。 rand() 是您正在寻找的函数。

如果您不知道要挑选什么种子,请使用当前时间:

srand(static_cast<unsigned int>(time(0))); // include <ctime> to use time()

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:

srand(static_cast<unsigned int>(time(0))); // include <ctime> to use time()
坚持沉默 2024-10-08 17:25:26

这样称呼它。

srand(9);
rand_int = rand();

call it this way.

srand(9);
rand_int = rand();
蒗幽 2024-10-08 17:25:26

您错误地使用了 srand,该特定函数用于为以后调用 rand 设置种子。

基本思想是使用不确定的种子调用 srand 一次,然后连续调用 rand 以获得数字序列。类似于:

srand (time (0));
for (int i = 0; i < 10; i++)
    cout << (rand() % 10);

它应该给你一些 0 到 9 之间的随机数(包括 0 和 9)。

通常,您不会将种子设置为特定值,除非您正在测试或出于其他原因需要相同的数字序列。您也不必每次在调用 rand 之前设置种子,因为您可能会重复获得相同的数字。

所以你的特定 while 循环更像是:

srand (9); // or use time(0) for different sequence each time.
while (close != "y") {  // for 1 thru 9 inclusive.
    rand_int = rand() % 9 + 1;
    cout << rand_int << endl;

    cout << "Do you wish to exit the program? [y/n]? ";
    cin >> close;
}

You're using srand incorrectly, that particular function is for setting the seed for later calls to rand.

The basic idea is to call srand once with an indeterminate seed, then call rand continuously to get a sequence of numbers. Something like:

srand (time (0));
for (int i = 0; i < 10; i++)
    cout << (rand() % 10);

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 (9); // or use time(0) for different sequence each time.
while (close != "y") {  // for 1 thru 9 inclusive.
    rand_int = rand() % 9 + 1;
    cout << rand_int << endl;

    cout << "Do you wish to exit the program? [y/n]? ";
    cin >> close;
}
心头的小情儿 2024-10-08 17:25:26

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文