• 13
• 18
• 19
• 27
• 10

# Shuffling infinite loop[solved]

This topic is 3246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Thanks, this is solved. [Edited by - lefthandman on April 29, 2009 9:44:22 PM]

##### Share on other sites
First: Fix the width of the codebox in your post!

A better way to shuffle the deck is by:
1. filling the deck with all cards in order.
2. pick a random card out of the deck and move it to the end of the deck
3. pick a random card out of the deck excluding the last card (the one that moved there in the previous step) and move it to the end of the deck.
4. pick a random card out of the deck excluding the last two cards and move it to the end of the deck
5. ... and so on until the remaining part of deck to pick a card from has just one card left.

This way it is finite.

##### Share on other sites
In C++ also:

1) fill the deck with all cards in order
2) use std::random_shuffle to put them in random order

That is also probably the longest condition I've ever seen: it fills more than a page and comes with embedded comments? :)

##### Share on other sites
In that gigantic condition, you include
rnumber[0] == rnumber[0][i-4]

Since you're looping from 0..12, but you only change the first four, what happens when i==8? [0][8] and [0][4] are both initialized to 0 previously.

Also, you are using negative indices, so long as i<12. Not good.

Also, if you need a set of unique numbers, it's better to generate them first and then shuffle them. It's an even easier task if you know the numbers you need.

D'oh, thank you!