为什么不改变结果?

时间:2015-07-11 21:21:18

标签: c++ srand

***问题已经回答,问题的关键是书的错误 "编程原理和实践使用C ++"表示srand可以接受与头文件中定义的函数randint()一起使用。

我希望答案不是太明显,但我已经环顾四周,似乎无法弄清楚为什么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";
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

除了值之外,将种子传递给默认引擎以进行初始化。我希望它能解决问题