简体   繁体   English

如何在 c++ 中使用“srand”,而不会引发 stackoverflow 异常?

[英]How can I use "srand" in c++, without getting stackoverflow exception thrown?

Error message: Unhandled exception at 0x00F94619 in Cpluspluslearning.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00CD2FC0).错误消息:Cpluspluslearning.exe 中 0x00F94619 处的未处理异常:0xC00000FD:堆栈溢出(参数:0x00000001、0x00CD2FC0)。

I am trying to make a deck of playing cards, to learn c++.我正在尝试制作一副扑克牌,学习 c++。 Everything works fine, and I am trying to make a method for shuffling the deck.一切正常,我正在尝试制定一种洗牌的方法。 I realizes that "rand" needs to be seeded, to be "truly" random.我意识到需要播种“rand”,才能“真正”随机。 But when I use the "srand" method i recieve the error message stated above.但是当我使用“srand”方法时,我会收到上述错误消息。 With no srand it works fine, but is not actually random.没有 srand 它可以正常工作,但实际上并不是随机的。 As this is my first C++ project, I would love some help.由于这是我的第一个 C++ 项目,我希望得到一些帮助。

Sincerely, sad person.真诚的,悲伤的人。

Code:代码:

#include <iostream>
#include <algorithm>
#include <ctime>

using namespace std;

//-------------------Classes-----------------------

class Card {
public:
    string suit, color, name;
    int value;

    Card(string x, string y, string q, int z) {
        suit = x;
        color = y;
        value = z;
        name = q;
    }
    Card() {}

    string getSuit() {
        return suit;
    }

    string getColor() {
        return color;
    }

    string getName() {
        return name;
    }

    int getValue() {
        return value
;
    }

    string toString() {
        return name + " of " + suit;
    }

};

//-----------------Variables----------------

Card deck[52];
string suits[] = {"Hearts","Spades","Diamonds","Clubs"};
string color[] = {"Red","Black"};
string name[] = { "Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King" };

//------------------Methods----------------

void createDeck() {

    int count = 0;
    for (int i = 0; i <= 51; i++) {
        
        if (count == 13) { count = 0;}
        
        if (i <= 12) {
            deck[i] = Card(suits[0], color[0], name[count], count + 1);
        }
        else if (i <= 25) {
            deck[i] = Card(suits[1], color[1], name[count], count + 1);
        }
        else if (i <= 38) {
            deck[i] = Card(suits[2], color[0], name[count], count + 1);
        }
        else {
            deck[i] = Card(suits[3], color[1], name[count], count + 1);
        }
        count++;
    }
    
}

int randomNumber(int array[],int size) {
    int random;

    srand(time(NULL));
    random = rand() % 52;
    
    for (int i = 0; i < size; i++) {
        if (array[i] == random) {
            randomNumber(array, size);
        }
    }
    return random;
}

void shuffleDeck() {
    int indexes[52];
    Card shuffledDeck[52];

    for (int i = 0; i <= 51; i++) {
        indexes[i] = randomNumber(indexes, 51);
        
        shuffledDeck[i] = deck[indexes[i]];
    }

    for (int i = 0; i < 51; i++)
    {
        deck[i] = shuffledDeck[i];
        cout << deck[i].toString() + " ";
    }
    
}

void game() {
    createDeck();
    shuffleDeck();
}

//-----------------Main------------

int main() {
    game();
    return 0;
}
indexes[i] = randomNumber(indexes, 51);

should be应该

indexes[i] = randomNumber(indexes, i);

because the rest of the array is uninitialized so far.因为阵列的 rest 到目前为止尚未初始化。

Also the "try again case"还有“重试案例”

randomNumber(array, size);

needs to be需要是

return randomNumber(array, size);

otherwise you'll end up returning the original duplicate number anyway.否则无论如何你最终都会返回原来的重复号码。

As a bonus, this fix will make the function tail-recursive, which means with a good compiler and tail-call optimization enabled, there will be no stack growth.作为奖励,此修复将使 function 尾递归,这意味着启用良好的编译器和尾调用优化,不会有堆栈增长。

In addition, you really want to move the srand(time(NULL)) call out of the loop as was already commented otherwise you generate the exact same random number a whole bunch of times before the computer clock ticks and gives you a different time(NULL) value.此外,您真的想将srand(time(NULL))调用移出循环,因为已经评论过,否则您会在计算机时钟滴答声之前生成完全相同的随机数很多次,并为您提供不同的time(NULL)值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM