35

OK, I had a hard time coming up with a single sentence title, so please bear with me.

Let's assume I have a computer with a perfect random number generator. I want to draw from a (electronic) deck of cards that have been shuffled. I can see two distinct algorithms to accomplish this:

  1. Fill a list with the 52 cards in random order, and then pull cards from the list in sequence. That is, defining the (random) sequence of cards before getting them. This is analogous to flipping over cards from a the top of a well-shuffled deck.

  2. Generate a random card from the set that hasn't been selected yet. In other words, you don't keep track of what card is going to come up next, you do a random select each time.

Programattically I can see advantages to both systems, but I'm wondering if there's any mathematical or statistical difference between them.

top 16 comments
sorted by: hot top controversial new old
[-] catloaf@lemm.ee 20 points 1 month ago

Mathematically, no, there is no difference.

However, if you're playing a card game that requires deck manipulation, you will of course want to generate the whole deck at once, because you might need to perform actions like placing a card from your hand on the bottom of the deck. You can't do that if the rest of the deck doesn't exist yet.

If you're generating cards at draw time, it can get expensive to check to see if a card has been generated yet, because you'll have to check the table, everyone's hand, and the discard pile. Or maintain a separate list of all cards generated so far, but at that point you might as well have just generated a whole deck in the first place.

[-] swordgeek@lemmy.ca 1 points 1 month ago

if you’re playing a card game that requires deck manipulation...

Ah! This is something that I hadn't directly considered. Interesting point, and it could be a serious concern for some games.

Thanks!

[-] ptz@dubvee.org 15 points 1 month ago* (last edited 1 month ago)

Assuming I'm understanding your thought experiment correctly, AFAIK, unless the chance of a duplicate card coming up is an issue, it should be about the same.

QI did a bit about this:

The chances that anyone has ever shuffled a pack of cards in the same way twice in the history of the world are infinitesimally small, statistically speaking. The number of possible permutations of 52 cards is '52 factorial' otherwise known as 52! or 52 shriek. This is 52 times 51 times 50 . . . all the way down to one. Here's what that looks like: 80,658,175,170,943,878,571,660,636,856,403,766,975,289,505,440,883,277,824,000,000,000,000.

To give you an idea of how many that is, here is how long it would take to go through every possible permutation of cards. If every star in our galaxy had a trillion planets, each with a trillion people living on them, and each of these people has a trillion packs of cards and somehow they manage to make unique shuffles 1,000 times per second, and they'd been doing that since the Big Bang, they'd only just now be starting to repeat shuffles.

https://www.youtube.com/watch?v=SLIvwtIuC3Y

[-] themeatbridge@lemmy.world 19 points 1 month ago* (last edited 1 month ago)

There's one quibble about this that might actually alter the answer for OP. Your numbers are correct for purely random shuffles, but a shuffle isn't purely random. A significant number of shuffles begin with a fresh deck where the cards are arranged in order and by suit. And a significant number of shufflers use the same methods, specifically the riffle, the overhand, or some other variant of those.

This is relevant to the field of cryptography, because humans are bad at simulating randomness. If you were to draw the top card from a shuffled deck, you probably have a lower chance of drawing an ace of spades than any other card, simply because the shuffler would not feel like they had shuffled if it was still on top. When picking lotto numbers, people tend to spread out their picks to get a quasi-even distribution across the available numbers, but it's just as likely to come up 1, 2, 3, 4, 5, and 6 as any other combination of numbers. Likewise, people playing with cards tend to cluster suits or pairs together when playing, so you'd be more likely to see those cards in proximity depending on the type of shuffle. Overhand tends to keep clumps together, and riffle or weave tend to interlace the cards in a way that still keeps pairs, sets, and runs close.

Of course, the difference would only be mathematically interesting, since in practice a good few shuffles is certainly close enough to completely random, especially if you start with a previously-shuffled deck. If you were trying to predict a card, your odds on any given draw is still going to be roughly 1 in 52.

[-] Subtracty@lemmy.world 4 points 1 month ago

I have seen the QI clip a few times and never thought about this. Very interesting! Thank you for explaining it.

[-] Alexstarfire@lemmy.world 6 points 1 month ago

No. Either way, you have the same set to pull from and your only pulling one card. The only difference is that generating a card means you don't have a set order when the first one is pulled.

If you wanted an equivalent with physical cards you'd pull a card, shuffle the rest, pull another card, repeat until the deck runs out.

[-] Willie@lemmy.world 4 points 1 month ago* (last edited 1 month ago)

Well, a little bit of 'yes' and a little bit of 'no'.

If it is possible over the course of the game for turn orders to be changed, or for a player to choose to draw a card, or cause another player to draw a card, then it matters in a way.

If I can cause actions to make another player draw a card, then it is more meaningful if the deck is shuffled already, because the card that I caused the player to draw is the same as the card I would have drawn if I drawn a card instead. However, from the perspective of the user, there is no way for them to know the difference.

I feel like it is better for the integrity of the game if the deck is shuffled for real, though. Because if ever a user finds out that it doesn't work how it is expected, then it cheapens the experience in a way. Kind of like how the old Mario Party games determined the outcome of dice rolls when the die appeared on screen instead of when you pressed the button to 'roll' them.

[-] swordgeek@lemmy.ca 2 points 1 month ago

If it is possible over the course of the game for turn orders to be changed, or for a player to choose to draw a card, or cause another player to draw a card, then it matters in a way.

This is a fascinating wrinkle, and quite correct. (and non-obvious, I would say.)

Everything that people have been posting leans towards laying out the order ahead of time, even if there's no mathematical difference in drawing the next card.

(Unless you're not dealing with a deck of cards, but instead 50k decks at once. :-D )

[-] Nougat@fedia.io 3 points 1 month ago

... you do a random select each time.

Is that even possible? I know that computers are not able to make true randomness, and that people are even worse at it. There's that lava lamp wall that somebody uses, maybe?

[-] ricdeh@lemmy.world 3 points 1 month ago

Computers are able to "make true randomness" if you give them the appropriate sensors and hardware, leveraging physical phenomena. Regardless, OP specified the following:

Let's assume I have a computer with a perfect random number generator.

[-] calcopiritus@lemmy.world 1 points 1 month ago

The lava lamps are not true random though. For something to be truly random, it must be non-deterministic (no seed at all). The only way for a computer to accomplish this is to read from a source of true randomness in nature. The lava lamps are random enough, but not truly random.

At the moment, the only source thought of being non-deterministic is quantum mechanics.

So if you make a computer generate random numbers out of the randomness of quantum mechanics, you would have truly random numbers.

[-] theilleists@lemmy.world 1 points 1 month ago

And even then, if you look at quantum mechanics through the right lens, its apparent randomness is only an illusion of perspective. If you flip the quantum coin, then with 100% certainty, perfectly deterministically, it will come up heads in one timeline and tails in the other. It's only because your two future selves can't interact with each other that they can't have an argument about what the result "really" was, so one says, "it actually came up heads, and the result was completely random," and the other says, "it actually came up tails, and the result was completely random."

[-] praise_idleness@sh.itjust.works 2 points 1 month ago

Either way, you pick a random number and get the corresponding card from a deck. In the single instance of drawing a card, there is absolutely no difference whatsoever.

[-] TootSweet@lemmy.world 2 points 1 month ago* (last edited 1 month ago)

Yeah, #2 is both more space efficient and more time efficient.

How I'd generally do something like that:

  1. Create an empty linked list.
  2. Generate a random integer between 0 and 51 inclusive.
  3. Iterate over the linked list and increment the random integer by one for each integer in the linked list less than or equal to your newly-generated random integer. You can break out of that loop as soon as you hit the first integer in the linked list greater than the newly-generated integer.
  4. Binary insert that integer into the sorted linked list.
  5. For the denomination, output the newly-generated integer modulus 13 plus one. Translate 1 to ace, 11 to jack, etc.
  6. For the suit, output floor of the newly-generated integer divided by 4 plus one. (Translate zero to "hearts", one to "diamonds", etc.)
  7. Loop back to step 2 51 more times.

Step 3 can definitely be optimized much more with a B-tree and a little thought. If you want jokers included, it's pretty straightforward. (Just change step 2 to generate a random integer between 0 and 53 and tweak steps 5, 6, and 7.)

[-] Brokkr@lemmy.world 5 points 1 month ago

I think you're approach is generally correct, but you've made a few errors which make it hard to follow (e.g. mixing up suit and denomination).

However, method two is only more efficient if onky a few cards will be drawn. If nearly the entire deck is drawn or dealt, then 1 is superior. Method 1 can be done with two lists and a random number generator. The length of the 2 lists will always sum to 52 and the RNG is used to decide the order that cards are removed from the first list and added to the 2nd. It requires generating at most 51 random numbers.

[-] TootSweet@lemmy.world 2 points 1 month ago

Good call on both counts!

I went ahead and fixed the suit/denomination mixup. I'll leave the reast as-is so folks can learn from my mistakes and your post continues to make sense.

Cheers!

this post was submitted on 23 Sep 2024
35 points (100.0% liked)

Ask Science

8594 readers
1 users here now

Ask a science question, get a science answer.


Community Rules


Rule 1: Be respectful and inclusive.Treat others with respect, and maintain a positive atmosphere.


Rule 2: No harassment, hate speech, bigotry, or trolling.Avoid any form of harassment, hate speech, bigotry, or offensive behavior.


Rule 3: Engage in constructive discussions.Contribute to meaningful and constructive discussions that enhance scientific understanding.


Rule 4: No AI-generated answers.Strictly prohibit the use of AI-generated answers. Providing answers generated by AI systems is not allowed and may result in a ban.


Rule 5: Follow guidelines and moderators' instructions.Adhere to community guidelines and comply with instructions given by moderators.


Rule 6: Use appropriate language and tone.Communicate using suitable language and maintain a professional and respectful tone.


Rule 7: Report violations.Report any violations of the community rules to the moderators for appropriate action.


Rule 8: Foster a continuous learning environment.Encourage a continuous learning environment where members can share knowledge and engage in scientific discussions.


Rule 9: Source required for answers.Provide credible sources for answers. Failure to include a source may result in the removal of the answer to ensure information reliability.


By adhering to these rules, we create a welcoming and informative environment where science-related questions receive accurate and credible answers. Thank you for your cooperation in making the Ask Science community a valuable resource for scientific knowledge.

We retain the discretion to modify the rules as we deem necessary.


founded 1 year ago
MODERATORS