Sign in to follow this  
lefthandman

Shuffling infinite loop[solved]

Recommended Posts

ernow    732
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 this post


Link to post
Share on other sites
visitor    643
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 this post


Link to post
Share on other sites
erissian    727
In that gigantic condition, you include

rnumber[0][i] == 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this