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

  • Home
  • SEARCH
  • 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 296623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:33:55+00:00 2026-05-12T06:33:55+00:00

I have an array which tells whether a card is in use: int used[52];

  • 0

I have an array which tells whether a card is in use:

int used[52];

This is a terrible way to pick a random card if I have many used cards:

do {
  card = rand() % 52;
} while (used[card]);

since if I have only 3-4 unused cards, it’ll take forever to find them.

I came up with this:

 int card;
 int k = 0;
 int numUsed = 0;
 for (k=0; k < 52; ++k) {
   if (used[k]) numUsed += 1;
 }
 if (numUsed == 52) return -1;
 card = rand() % (52 - numUsed);

 for (k=0; k < 52; ++k) {
   if (used[k]) continue;
   if (card == 0) return k;
   card -= 1;
 }

which I guess works better if the deck is full, but works worse when the deck is empty since I have to go through two for loops.

What’s the most efficient way to do this?

  • 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-12T06:33:56+00:00Added an answer on May 12, 2026 at 6:33 am

    I think your two-pass algorithm is likely to be the best you can do, given the constraint you added in a comment that you don’t know in advance which cards are eligible for a given draw.

    You could try the cunning “select at random from a list of unknown size in a single pass” algorithm:

    int sofar = 0;
    int selected = -1;
    for (i = 0; i < 52; ++i) {
        if (used[i]) continue;
        ++sofar;
        if ((rand() % sofar) == 0) selected = i;
    }
    if (selected == -1) panic; // there were no usable cards 
    else used[selected] = 1;   // we have selected a card
    

    Then if (as you say in a comment) different draws have different criteria, you can replace used[i] with whatever the actual criteria are.

    The way it works is that you select the first card. Then you replace it with the second card with probability 1/2. Replace the result with the third card with probability 1/3, etc. It’s easy to prove by induction that after n steps, the probability of each of the preceding cards being the selected one, is 1/n.

    This method uses lots of random numbers, so it’s probably slower than your two-pass version unless getting each item is slow, or evaluating the criteria is slow. It’d normally be used e.g. for selecting a random line from a file, where you really don’t want to run over the data twice. It’s also sensitive to bias in the random numbers.

    It’s good and simple, though.

    [Edit: proof

    Let p(j,k) be the probability that card number j is the currently-selected card after step k.

    Required to prove: for all n, p(j,n) = 1/n for all 1 <= j <= n

    For n = 1, obviously p(1,1) = 1, since the first card is selected at the first step with probability 1/1 = 1.

    Suppose that p(j,k) = 1/k for all 1 <= j <= k.

    Then we select the (k+1)th card at step (k+1) with probability 1/(k+1), i.e p(k+1,k+1) = 1/(k+1).

    We retain the existing selection with probability k/(k+1), so for any j < k+1:

    p(j,k+1) = p(j,k) * k/(k+1)
             = 1/k    * k/(k+1)   // by the inductive hypothesis
             = 1/(k+1)
    

    So p(j,k+1) = 1/(k+1) for all 1 <= k <= k+1

    Hence, by induction, for all n: p(j,n) = 1/n for all 1 <= j <= n]

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

Sidebar

Ask A Question

Stats

  • Questions 155k
  • Answers 155k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are no real hard and fast rules for this.… May 12, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer One error I see repeated over and over again: Instantiating… May 12, 2026 at 10:47 am
  • Editorial Team
    Editorial Team added an answer I ran into a similar problem when attempting to bind… May 12, 2026 at 10:47 am

Related Questions

I'm having a problem with an application hanging and giving me the default Please
I have a method fetchObjects(String) that is expected to return an array of Contract
A newbie question from me, please. I have a large data set that I
This is mostly a theoretical question I'm just very curious about. (I'm not trying
I came across something very basic but extremely bewildering today. I needed to convert

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.