Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:25:37+00:00 2026-06-02T00:25:37+00:00

How do I generate a new random number if validation is 1? Can I

  • 0

How do I generate a new random number if validation is 1?

Can I just put another “generate number line”: cardDrawn=(1+(rand()%52));

#include <iostream>;
#include <cstdlib>;
#include <ctime>;
#include <cmath>;

using namespace std;

int main(){
int deck[52];
bool validate[52]={0};
int hand[5];
int cardDrawn;
int count=0;
int j=0;
bool repeat=0;
string cardNames[52]=   {"Ace of Clubs","2 of Clubs","3 of Clubs","4 of Clubs","5 of Clubs","6 of Clubs","7 of Clubs","8 of Clubs","9 of Clubs","10 of Clubs","Jack of Clubs","Queen of Clubs","King of Clubs",//CLUBS
                        "Ace of Spades","2 of Spades","3 of Spades","4 of Spades","5 of Spades","6 of Spades","7 of Spades","8 of Spades","9 of Spades","10 of Spades","Jack of Spades","Queen of Spades","King of Spades",//SPADES
                        "Ace of Hearts","2 of Hearts","3 of Hearts","4 of Hearts","5 of Hearts","6 of Hearts","7 of Hearts","8 of Hearts","9 of Hearts","10 of Hearts","Jack of Hearts","Queen of Hearts","King of Hearts",//HEARTS
                        "Ace of Diamonds","2 of Diamonds","3 of Diamonds","4 of Diamonds","5 of Diamonds","6 of Diamonds","7 of Diamonds","8 of Diamonds","9 of Diamonds","10 of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds"//DIAMONDS
                        };//array to hold card abbreviations.

srand(time(0));
for (int i=0; i<52; i++){  // fills arrays with 1-52
deck[i]=i+1;};


cardDrawn=(1+(rand()%52));  //generates first card.
hand[0]=cardDrawn;
validate[cardDrawn]=1;
//cout << "Card 1: " << hand[0];

do{
    cardDrawn=(1+(rand()%52));
    if (validate[cardDrawn]==1)
        {
            //count doesn't update!  :D
        }
    else
        {
            hand[count]=cardDrawn;
            count++;
        }
     }while(count<4);

cout << "Card 1: " << cardNames[hand[0]];
cout << "\ncard 2: " << cardNames[hand[1]];
cout << "\ncard 3: " << cardNames[hand[2]];
cout << "\ncard 4: " << cardNames[hand[3]];
cout << "\ncard 5: " << cardNames[hand[4]];

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:25:39+00:00Added an answer on June 2, 2026 at 12:25 am

    The effect you wish to achieve is much easier using some STL. You can easily extend it to any number of cards drawn (within the same deck) with the same complexity as drawing the first.

    In fact, I added 4 players, each having 5 cards in their hand. It’s fairly easy to scale down to just one player. I also simplified the process of adding names for every card.

    #include <algorithm> //random_shuffle
    #include <cstdlib> //srand
    #include <ctime> //time
    #include <iostream> //cout
    #include <string> //string
    #include <vector> //vector
    
    using namespace std;
    
    int main()
    {
        srand (time (NULL)); //so random_shuffle gives different values
    
        const vector<string> values = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
        const vector<string> suits = {"Clubs", "Spades", "Hearts", "Diamonds"};
    
        vector<string> cards; //for names of cards
    
        for (int suit = 0; suit < 4; ++suit)
        {
            for (int value = 0; value < 13; ++value)
            {
                cards.push_back (values [value] + " of " + suits [suit]); //combine suit and value
            }
        }
    
        vector<vector<string>> playerHands (4); //4 players' hands
    
        random_shuffle (cards.begin(), cards.end()); //shuffle cards
    
        for (auto &hand : playerHands) //for each hand
        {
            for (int card = 0; card < 5; ++card) //for 5 cards
            {
                hand.push_back (cards.back()); //add last card to hand
                cards.pop_back(); //remove last card from deck
            }
        }
    
        int playerCount = 1; //for player's number
        for (auto hand : playerHands) //for each hand
        {
            cout << "Player " << playerCount << ":\n"; //output player number
    
            int handCount = 1; //for card's number
            for (auto card : hand) //for each card in hand
            {
                cout << "Card " << handCount << ": " << card << '\n'; //output card
                ++handCount; //increase card number
            }
    
            ++playerCount; //increase player number
            cout << '\n'; //separate players
        }
    }
    

    You could also either generate the list of card names each time the deck runs out, or just use a copy of cards for each deck (vector<string> currentDeck (cards);).

    If this doesn’t compile at all, you need C++11 and the appropriate compiler option. Otherwise, for (... : ...) can be replaced with std::for_each, auto can be replaced with the type, and the vector initializer lists can be replaced with a different method of constructing the vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to generate a random number. Random R = new Random(0);
In C# I can generate random number from a string like this: public string
I found this answer in another post, on how to generate a random number:
How to generate 6 dig random number (within specific interval)? would Random rand =
If my math is right, I can quickly generate a new hash value for
I want to generate random number, which is 9 digits including leading zero if
Possible Duplicate: Java: generating random number in a range I want to generate a
I know how to generate a random number between 0 and 1 using the
i am trying to generate random number for my mental math quiz game. But
Possible Duplicate: Java: generating random number in a range How do I generate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.