Jump to content
  • Advertisement
Sign in to follow this  
lefthandman

Shuffling infinite loop[solved]

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!