Shuffling infinite loop[solved]

Started by
4 comments, last by yaustar 14 years, 11 months ago
Thanks, this is solved. [Edited by - lefthandman on April 29, 2009 9:44:22 PM]
ROFLMAO-GG-HF-GL-LOL-TTYL-BRB-GTG
Advertisement
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.
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? :)
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.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
D'oh, thank you!
ROFLMAO-GG-HF-GL-LOL-TTYL-BRB-GTG
Quote:Original post by lefthandman
D'oh, thank you!

Next time, please don't remove the original problem as others can't from this at a later date.

Steven Yau
[Blog] [Portfolio]

This topic is closed to new replies.

Advertisement