【问题标题】:Why isn't srand seed changing results?为什么 srand 种子不改变结果?
【发布时间】:2015-07-11 21:21:18
【问题描述】:

我希望答案不会太明显,但我环顾四周,似乎无法弄清楚为什么 srand 没有改变 Bull_pen 向量中的值。 每次运行时,我都会得到相同的 4 个整数。

我已经阅读了所有关于使用计算机时钟的信息,确保只使用 srand 一次或在循环之外等,包括 stdlib.h,但这些都没有帮助。 使用用户输入作为种子似乎很容易,但现在我觉得我错过了一些 某处的基本了解。 如果我的风格很差等,我提前道歉——对于 C++ 和一般编程来说都是新手。感谢您的帮助。

#include "..\..\std_lib_facilities.h"

int main()
{
    int seed;
    cout << "Before the game begins, enter any integer.\nThis will generate a seed to randomize the game: ";
    cin >> seed;
    srand(seed);
    cout << "I have four numbers in a sequence for you to guess." << endl
        << "For every digit you guess that matches a digit in my sequence, I will tell you\nthat you guessed that many 'cows'." << endl
        << "For ever digit you guess that matches a digit AS WELL as matches the digits\nlocation in my sequence, I will say you guessed" << endl
        << "that many 'bulls'. Guessing all four 'bulls' wins the game.\n";

    while (1 == 1){

        vector<int>bull_pen(4);
        bull_pen[0] = randint(9);
        bull_pen[1] = randint(9);
        bull_pen[2] = randint(9);
        bull_pen[3] = randint(9);

        vector<int>guesses(4);
        int guess;


        int found = 0;
        int bulls = 0;
        int cows = 0;

        while (found != 1){
            vector<int>bull_pen_flags(4);

            cout << "Please enter your guesses: ";
            for (int i = 0; i < guesses.size(); i++){
                cin >> guess;
                guesses[i] = guess;
            }

            for (int i = 0; i < bull_pen.size(); i++){
                if (bull_pen[i] == guesses[i]) { bulls++; bull_pen_flags[i] = 1; }
            }

            if (bulls < 4){
                for (int guess_index = 0; guess_index < guesses.size(); guess_index++){
                    for (int bull_index = 0; bull_index < bull_pen.size(); bull_index++){
                        if (guess_index != bull_index && bull_pen_flags[bull_index] != 1){
                            if (guesses[guess_index] == bull_pen[bull_index]) { 
                            cows++;
                            bull_pen_flags[bull_index] = 1;
                        }
                        }
                    }
                }


                if (cows > 0) cout << cows << " cow(s).\n";
                else cout << "No cows.\n";
                if (bulls > 0) cout << bulls << " bull(s).\n";
                else cout << "No bulls.\n";
                bulls = 0;
                cows = 0;
            }

            else found = 1;

        }
        cout << "You've guessed all four bulls; " << bull_pen[0] << bull_pen[1] << bull_pen[2] << bull_pen[3] << "\nYou win!\n";
    }
}

【问题讨论】:

  • 什么是randint?它是如何定义的?
  • 不知道。它可能来自我正在使用的头文件,我从一本用于学习 C++ 的书中获取。有没有更通用的选择?我见过 rand 的用法,所以我觉得用 rand 替换 randint 会有帮助吗?
  • 如果您输入相同的seed,那么rand() 每次都会产生相同的序列。这就是 rand() 的定义方式。通常,您会使用srand(time(0)); 来播种,以便每次运行它时,它都会产生不同的序列。
  • 不管种子的不同输入我得到相同的结果,我也尝试过 srand(time(NULL)) 并根据您的建议 srand(time(0)) 并且仍然是相同的结果 -在这里挠头..
  • 我想您正在关注 Bjarne Stroustrup 的“使用 C++ 进行编程原理和实践”?

标签: c++ srand


【解决方案1】:

randint 不是标准库函数。它是您正在使用的头文件中的一个函数,它不是标准库的一部分。它只是 Bjarne Stroustrup 的“使用 C++ 的编程原理和实践”的介绍性库的一部分。源文件为here,该函数的定义如下:

inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }

inline int randint(int max) { return randint(0, max); }

如您所见,它使用default_random_engine 生成随机数,这是 C++11 标准库的一部分。 srand 对此引擎没有影响。有一种方法可以播种它,但 Bjarne 并没有通过他提供的界面公开它(我认为他只是试图在这个库中保持尽可能简单,因为它只是用于介绍目的)。

如果您想要一个可以播种的随机数生成器,那么您可以继续使用srand 进行播种,但使用rand() 来生成您的数字。更好的是,您可以使用 randint 函数使用的同一引擎。

std::default_random_engine engine(seed);
std::uniform_int_distribution<> dist(0, 9);

int x = dist(engine);

【讨论】:

  • 嗯……书上说:您可以通过四次调用 std_lib_facilities.h 中的随机数生成器 randint(10) 来获得四个随机数。您会注意到,如果您重复运行该程序,则每次启动该程序时它都会选择相同的四位数字序列。为避免这种情况,请让用户输入一个数字(任何数字)并调用 srand(n),其中 n 是用户在调用 randint(10) 之前输入的数字。这样的 n 称为种子,不同的种子给出不同的随机数序列。
  • @BlueMoon:您必须使用旧版本,它使用rand。如果较新的版本仍然这样说,那就是一个错误。
  • 是的,我有旧的。当我得到更新的版本时,我会验证你所说的:D
  • 如果最新版本是第二个,那么我有它,它说:“你会注意到,如果你重复运行那个程序,它每次你都会选择相同的四位数字序列启动程序。为避免这种情况,请用户输入一个数字(任何数字)并调用 srand(n),其中 n 是用户在调用 randint(10) 之前输入的数字。这样的 n 称为种子,并且不同种子给出不同的随机数序列。”无论如何,rand() 有效地解决了问题,谢谢!
  • @BrendanNolan:封面上的白圈是不是写着“使用C++11和C++14”?
【解决方案2】:
inline int randint(int min, int max,int seed) { static default_random_engine ran(seed);
    return uniform_int_distribution<>{min, max}(ran); }

inline int randint(int max,int seed) { return randint(0, max,seed); }

您可以修改头文件,就像上面一样,将种子传递给默认引擎。这是我的程序的版本

#include "std_lib_facilities.h"

int main (){
    cout << "Enter any number to start the game";
    int seed;
    cin >> seed;
    vector<int> v(4);

    // For generating randon numbers

    for (int i = 0; i < v.size(); i++)
        v[i] = randint(9,seed);

    // For generating random four different digits

    for (int i = 1 ; i < v.size(); i++){
        for (int j = i; j < v.size(); j++){
            while (v[i-1] == v [j])
                v[i-1] = randint(9,seed);
        }
    }
    int bull = 0, cow = 0, tries = 0;
    cout << "I have a four digit number can you guess it??\n";
    cout << "lets start the game!!!\n ";
    int number;
    while (bull != 4){
        tries++;
        bull = 0;
        cow = 0;
        cout << "Guess the number: ";
        cin >> number;
        if (number > 1000 && number < 9999){

        for (int i = 0; i < v.size(); i++){
                if (v[i] == number % 10){ // to see digit is correct
                    if (i == v.size()-1)// to see 1's position is correct
                        bull++;
                    else cow++;
                }
                if (v[i] == (number % 100) / 10){ // to see digit is correct
                    if (i == v.size()-2)// to see 10's position is correct
                        bull++;
                    else cow++;
                }
                if (v[i] == (number / 100) % 10){ // to see digit is correct
                    if (i == v.size()-3)// to see 100's position is correct
                        bull++;
                    else cow++;
                }
                if (v[i] == number / 1000){ // to see digit is correct
                    if (i == v.size()-4)// to see 1000's position is correct
                        bull++;
                    else cow++;
                }

        }

        cout << "Digits correct = " << bull+cow << "\n" << "Position correct = " << bull << "\n";
        }
        else
            cout << "Enter only four digit numbers\n";
    }
    cout << "CONGRATS!!!..YOU GUESSED THE NUMBER IN  " << tries << " tries\n";
    return 0;
}

与值一起,将种子传递给运行的默认引擎进行初始化。希望能解决问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-20
    • 2017-01-08
    • 2018-03-21
    • 1970-01-01
    • 2010-09-20
    • 2018-06-05
    • 2011-07-31
    • 1970-01-01
    相关资源
    最近更新 更多