aspose file tools*
The moose likes Beginning Java and the fly likes Card shuffling on ArrayList hangs.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Card shuffling on ArrayList hangs.." Watch "Card shuffling on ArrayList hangs.." New topic
Author

Card shuffling on ArrayList hangs..

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

My card shuffling algorithm seems to be hanging up my machine quite a bit, any idea why?



Jon
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Check your nested loop. The inner loop guard may never end because it's guard (previousIndex == newIndex) may never become true. But let's assume that eventually it will. Then your outer loop's guard (swapCount < number_of_swaps) will never become true because you never change swapCount.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

You don't appear to be incrementing swapCount anywhere, which is probably why it hangs - the loop never ends.

That doesn't seem a very efficient shuffle algorithm, if you don't mind me saying - just randomly swapping pairs. For any given card, there's a reasonable chance it'll never be swapped. And if you want a better one - there happens to be one built in to the collections framework. You could swap your entire shuffle method for:
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Matthew Brown wrote:You don't appear to be incrementing swapCount anywhere, which is probably why it hangs - the loop never ends.

That doesn't seem a very efficient shuffle algorithm, if you don't mind me saying - just randomly swapping pairs. For any given card, there's a reasonable chance it'll never be swapped. And if you want a better one - there happens to be one built in to the collections framework. You could swap your entire shuffle method for:


You are right, in fact I had the 'Ace of Spades' listed twice on my algorithm somehow, even though it's the swapping algorithm I've been using since I was 12 years old. My algorithm has taken advantage of the existing Collections.shuffle() method:


By the way do you know of any IDE backed code where I can switch on and off debugging code, similarly to the way #if is used in C#? (http://msdn.microsoft.com/en-us/library/4y6tbswk.aspx).
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

Jon Camilleri wrote:By the way do you know of any IDE backed code where I can switch on and off debugging code, similarly to the way #if is used in C#?

I think the most common approach would be to use a logging framework such as log4j. Your log statements state the level they are at (e.g. "debug", "warning", "error"), and then a configuration file defines which levels should be output.

The advantage that has over conditional compilation is that it can be changed without recompiling, so you can turn on debugging on a deployed application. It also gives you lots of control over where the log is output - to the console, to a file, to a database etc.
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 180

I find that the suggest approach:

while being the simplest to implement, does not do a sufficient enough job of randomization for games. I find that, though the cards will be shuffled about, they tend to stay in the same general region of the deck. The approach I use is to, one by one, move a random card from one list to a new list.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

Carey Brown wrote:I find that the suggest approach:

while being the simplest to implement, does not do a sufficient enough job of randomization for games. I find that, though the cards will be shuffled about, they tend to stay in the same general region of the deck. The approach I use is to, one by one, move a random card from one list to a new list.

Really? The algorithm that the documentation describes wouild be perfectly random if the random number generator was perfectly random. In particular, it's actually functionally identical to the one you've just described - it shuffles in place but it performs the same logic:

This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive.


Do that, with a perfect generator, and all permutations have exactly the same likelihood.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Never write == true or == false. They are clumsy, and error-prone bits of code. You write if (shuffled) or if (!shuffled).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Card shuffling on ArrayList hangs..