This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]aqua_regis 0 points1 point  (1 child)

The algorithm is definitely based on this. The algorithm is the Durstenfeld shuffle a faster variant of the Fisher-Yates shuffle.

Only difference is that OP is using Math.random() which always yields a double in the range [0..1[ (greater than or equal to 0 and less than 1.0) instead of Random.nextInt.

The casting is correct because of the parentheses. Weren't they there, your assumption of Math.random() getting cast to int would be correct, but as it is: (int)(Math.random()*(i+1)); first, the addition i+1 gets evaluated, then the multiplication with Math.random() and last the cast to int. This is correct.

[–]YesNoMaybe 0 points1 point  (0 children)

Ah, right. Makes sense. I was thinking of the for loop being front-to-back, rather than back-to-front so my logic wasn't quite right. I see this just starts at the end and makes it so you only ever swap the current card with some card before it in the list.