C++ 一副扑克牌:我怎样才能做到只随机抽一张牌?

C++ 一副扑克牌:我怎样才能做到只随机抽一张牌?,c++,C++,我试图模拟一副牌,但我不知道如何制作,所以它随机选择一张牌,但只选择一次。我不断得到双倍的牌 #include <iostream> #include <cstdlib> //for rand and srand #include <cstdio> #include <string> using namespace std; string suit[] = { "Diamonds", "Hearts", "Spades", "Clubs" };

我试图模拟一副牌,但我不知道如何制作,所以它随机选择一张牌,但只选择一次。我不断得到双倍的牌

#include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
#include <string>

using namespace std;

string suit[] = { "Diamonds", "Hearts", "Spades", "Clubs" };
string facevalue[] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King", "Ace" };

string getcard() {
    string card;
    int cardvalue = rand() % 13;
    int cardsuit = rand() % 4;

    card += facevalue[cardvalue];
    card += " of ";
    card += suit[cardsuit];

    return card;
}

int main() {
    int numberofcards = 52;

    for (int i = 0; i < numberofcards; i++) {
        cout << "You drew a " << getcard() << endl;
    }

    system("pause");
}
#包括
#包括//用于兰德和srand
#包括
#包括
使用名称空间std;
弦乐套装[]={“钻石”、“红心”、“黑桃”、“梅花”};
字符串facevalue[]={“二”、“三”、“四”、“五”、“六”、“七”、“八”,
“九”、“十”、“杰克”、“女王”、“国王”、“王牌”};
字符串getcard(){
串卡;
int cardvalue=rand()%13;
int cardsuit=rand()%4;
卡片+=面值[cardvalue];
卡片+=“of”;
卡片+=套装[卡片套装];
回程卡;
}
int main(){
int numberofcards=52;
对于(int i=0;icout您需要模拟一副牌,以便在选择一张牌时,将其从牌列表中删除

#include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
#include <string>

using namespace std;

string suit[] = { "Diamonds", "Hearts", "Spades", "Clubs" };
string facevalue[] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King", "Ace" };

string getcard() {
    string card;
    int cardvalue = rand() % 13;
    int cardsuit = rand() % 4;

    card += facevalue[cardvalue];
    card += " of ";
    card += suit[cardsuit];

    return card;
}

int main() {
    int numberofcards = 52;

    for (int i = 0; i < numberofcards; i++) {
        cout << "You drew a " << getcard() << endl;
    }

    system("pause");
}

因此,当你从一张满牌开始,然后从列表中随机选择一张牌时,你会将其从列表中删除。

你需要模拟一副牌,这样当选择一张牌时,它就会从一张牌列表中删除

#include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
#include <string>

using namespace std;

string suit[] = { "Diamonds", "Hearts", "Spades", "Clubs" };
string facevalue[] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Jack", "Queen", "King", "Ace" };

string getcard() {
    string card;
    int cardvalue = rand() % 13;
    int cardsuit = rand() % 4;

    card += facevalue[cardvalue];
    card += " of ";
    card += suit[cardsuit];

    return card;
}

int main() {
    int numberofcards = 52;

    for (int i = 0; i < numberofcards; i++) {
        cout << "You drew a " << getcard() << endl;
    }

    system("pause");
}

因此,发生的情况是,您从一个完整的卡片组开始,然后从列表中随机选择一张卡片,然后将其从列表中删除。

您正在使用替换进行采样,这意味着一旦选择一张卡片,您就将其留在卡片组中。通过从数据结构中删除它,将其从卡片组中删除。您需要调整随机sa相应地,通过改变
cardvalue
cardsuit
的范围作为数组/向量的长度/任何改变来进行mpling。

你是用替换进行采样,这意味着一旦你选择了一张卡,你就把它留在了卡片组中。通过从数据结构中移除它,将其从卡片组中取出。你需要调整你的r相应地,通过改变
cardvalue
cardsuit
范围作为数组/向量的长度/任何变化来进行随机采样。

这是一副牌。只需执行以下操作:

  • 初始化牌组。在固定的52张牌阵列中布局所有52张牌
  • 洗牌
  • 通过将
    nextCard
    索引初始化到你的牌组中,从零(0)开始,开始绘制循环。每次“绘制”(牌组位于
    deck[nextCard]
    )将
    nextCard
    前进一步。当
    nextCard
    ==52时,你的牌就用完了
  • 下面是如何设置数据组的示例。我将
    nextCard
    索引和绘图算法留给您

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    // names of ranks.
    static const char *ranks[] =
    {
        "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
        "Eight", "Nine", "Ten", "Jack", "Queen", "King"
    };
    
    // name of suites
    static const char *suits[] =
    {
        "Spades", "Clubs", "Diamonds", "Hearts"
    };
    
    void print_card(int n)
    {
        cout << ranks[n % 13] << " of " << suits[n / 13] << endl;
    }
    
    int main()
    {
        srand((unsigned int)time(NULL));
    
        int deck[52];
    
        // Prime, shuffle, dump
        for (int i=0;i<52;deck[i++]=i);
        random_shuffle(deck, deck+52);
        for_each(deck, deck+52, print_card);
    
        return 0;
    }
    

    这是一副牌。只需这样做:

  • 初始化牌组。在固定的52张牌阵列中布局所有52张牌
  • 洗牌
  • 通过将
    nextCard
    索引初始化到你的牌组中,从零(0)开始,开始绘制循环。每次“绘制”(牌组位于
    deck[nextCard]
    )将
    nextCard
    前进一步。当
    nextCard
    ==52时,你的牌就用完了
  • 下面是如何设置数据组的示例。我将
    nextCard
    索引和绘图算法留给您

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    // names of ranks.
    static const char *ranks[] =
    {
        "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
        "Eight", "Nine", "Ten", "Jack", "Queen", "King"
    };
    
    // name of suites
    static const char *suits[] =
    {
        "Spades", "Clubs", "Diamonds", "Hearts"
    };
    
    void print_card(int n)
    {
        cout << ranks[n % 13] << " of " << suits[n / 13] << endl;
    }
    
    int main()
    {
        srand((unsigned int)time(NULL));
    
        int deck[52];
    
        // Prime, shuffle, dump
        for (int i=0;i<52;deck[i++]=i);
        random_shuffle(deck, deck+52);
        for_each(deck, deck+52, print_card);
    
        return 0;
    }
    

    选择后,删除。有什么好处吗?洗牌后将牌推入队列,然后从队列中抽牌。队列的数据结构可能比您最初想象的要简单。即,进入洗牌组的索引会随着每次抽牌向前移动。可能相关:选择后,删除。有什么好处吗?洗牌后将牌推入队列,然后从中抽取。队列的数据结构可能比您最初想象的要简单。例如,一个进入无序组的索引,随着每次抽取向前移动。可能相关:for(2)没有理由不使用适当的随机化算法。我特别喜欢这个答案,因为它更面向模拟:你永远不会丢失信息,所以你可以重放卡片的前进,如果你愿意,向后。@ AricTenEyck:特别是在C++ +Matt中,它甚至可以被利用,尤其是在你受限于BA的情况下。抽签的ck端。例如:你有一个游戏总是抽五张牌。一旦你达到50张,剩下的就不是五张了。在这一点上,你可以只重新洗牌[0..49]。如果你的
    nextCard
    increment实际上是'nextCard=(nextCard+1)%52,那么你现在有一个循环牌组,它总是随机启动的。顺便说一句,我已经做了很多次了没有理由不使用适当的随机化算法。我特别喜欢这个答案,因为它更面向模拟:你永远不会丢失信息,所以你可以重放卡片的前进,如果你愿意,向后。@ AricTenEyck:特别是在C++ +Matt中,它甚至可以被利用,尤其是在你受限于BA的情况下。抽签的ck端。例如:你有一个游戏总是抽五张牌。一旦你达到50张,就没有剩下五张牌了。在这一点上,你可以只重新洗牌[0..49]。如果你的
    nextCard
    增量实际上是'nextCard=(nextCard+1)%52,那么你现在有一个总是随机打底的圆形牌组。顺便说一句,我已经做了很多次了