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 7582415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:25:47+00:00 2026-05-30T18:25:47+00:00

Im learning basic C on my own and trying to create a poker client.

  • 0

Im learning basic C on my own and trying to create a poker client. I have an array with cards (called kortArray) and a players hand (called kortHand). My implementation does not shuffle the deck, but add all 52 cards in sequence and then randomly selecting 5 cards from the deck. I’ve added a flag (called draget) which tells if a card has been picked up befor or not.

Now, when I run the algorithm below, it usually generates five random numbers which makes the player’s or computer’s hand. But sometimes it generates less then five numbers, even though I’ve specifically stated to generate five accepted values.

I have two loops, one that runs five times, and the other is nested and runs until it finds a card which hasn’t yet been picked. The printf in the middle tells me that this algorithm doesn’t always generate five accepted numbers, and when that happens the player’s hand contains cards with nonsense-values.

srand((unsigned)(time(0)));
for(i = 0; i < 5; i++) {                        
    int x = rand()%52 + 1;
    while (kortArray[x].draget!=1) {                
        x = rand()%52 + 1;
        if (kortArray[x].draget != 1) {
            printf("%i\n", x);
            kortArray[x].draget = 1;
            kortHand[i] = kortArray[x];
        }
    }
}
  • 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-05-30T18:25:48+00:00Added an answer on May 30, 2026 at 6:25 pm

    The problem still lies in the +1 for the random numbers.

    Also, you are first checking in the the first assignment to x if the card is already picked, and than you assign it to an other card.

    Why not use something like:

    int nr_cards_picked = 0             /* Number of uniquely picked cards in hand */
    /* Continue picking cards until 5 unique cards are picked. */
    while (nr_cards_picked < 5) {
        x = rand() % 52;                         /* Take a random card */
        if (kortArray[x].draget == 0) {
            /* Pick this card. */
            kortArray[x].draget = 1;             /* Card is picked */
            kortHand[i]         = kortArray[x];  /* Add picked card to hand */
            nr_cards_picked++;
        }
    }
    

    Forgive compiler errors; I don’t have a compiler near here.

    This case you only have one time a random number call.

    Theoretically it might never end but this is not likely.

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

Sidebar

Related Questions

basic question - I'm still learning Android - trying to create a seperate thread
I am currently learning about basic networking in java. I have been playing around
I've been learning wpf for about a week now.. and i have a basic
I'm a JavaScript novice trying to build a basic share button as a learning
I'm trying to implement a basic jQuery infinite carousel. As much for the learning
I'm learning about SNMP, and writing some applications using it. I have some basic
I'm just learning some basic programming in Objective C and Cocoa. I'm trying to
Learning basic Objective-C and have a few beginner questions. How would I define and
I'm using learning the basic of boost.thread. So far, I can create each thread
I have been working on my own library/framework for the learning experience for 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.