shuffle a deck of 51 cards problem
Author |
Message |
addie1125

|
Posted: Thu Jan 15, 2009 10:00 am Post subject: shuffle a deck of 51 cards problem |
|
|
i am doing a procedure to shuffle a deck of 51 cards
this is a procedure in a class
this is not working
can you help me ? thank you very much ~>_<~
var Card : array 0 .. 51 of int % global variable for the class
procedure shuffleDeck
for i : 0 .. 51
Card(i) := i
end for
for decreasing j : 51 .. 0
k := Rand.Int (0, j)
Card(k) := Card(j)
end for
end shuffleDeck
-------------------------------------
the numbers this thing puts do not repeat...
for i : 0 .. 51
Card(i) := i
end for
for decreasing j : 51 .. 0
k := Rand.Int (0, j)
Card(k) := Card(j)
end for |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
chrisbrown

|
Posted: Thu Jan 15, 2009 11:32 am Post subject: RE:shuffle a deck of 51 cards problem |
|
|
1) Use code tags, i.e [_Code_] Some code [_/Code_] but without the underscores.
2) Look at your shuffle procedure- assuming you've defined k, you're setting some random card in the deck to be the jth (last, sort of) card in the deck, but you don't remove that jth card so thats why you have duplicates.
Visually, if k = x:
code: | [ ... x y z ... j ] | becomes code: | [ ... x j z ... j ] |
What you need to do is swap the cards, not just reassign one of them (each time around the loop).
3) The range 0 .. 51 contains 52 elements so you actually have a standard 52 card deck there.
4) The first for loop in shuffle is not needed. You don't need to reset your deck each time you shuffle it. |
|
|
|
|
 |
|
|