The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

# Thread: Help needed with a card shuffling function

1. 13
posts
Registered User

## Help needed with a card shuffling function

Hello all! I'm trying to make a game that makes primary use of a deck of cards, but right now I'm having trouble making the deck random. My attempted method is to pop or shift (based on a random roll) the contents of array1 into array2 and back for a total of about 5 swaps ultimately ending with a nicely randomized array1 in array1. I've tried a couple different methods, but it always ends up a direct copy of array1 in array2.

function shuffle(){
for(i=0; i<=5; i++){
if(i/2 % 0){
swap = 1
}else{
swap = 2
}
if(swap=1){
while(array1.length > 0){
num = random(2);
if(num=0){
array2[array2.length] = array1.pop();
}else if(num=1){
array2[array2.length] = array1.shift();
}
}
}else if(swap=2){
while(array2.length > 0){
num = random(2);
if(num=0){
array1[array1.length] = array2.pop();
}else if(num=1){
array1[array1.length] = array2.shift();
}
}
}
}
}
If anyone could tell me how to fix this code, or even better a more efficient way to write a shuffle function, I'd be greatly appreciative. Please do excuse the mess, this is my first attempt at a real program.

2. 150
posts
Registered User
here is one of many methods to suffle, not the best...

Code:
```var arr:Array = new Array("jedan",
"dva",
"tri",
"cetiri",
"pet",
"sest",
"sedam",
"osam",
"devet",
"deset");

var sArr:Array = new Array();

trace("Array : " + arr);
sArr = shuffle(arr);
trace("Shuffle : " + sArr);

function shuffle(arr:Array) : Array {
var tmp:Array = new Array();
while(arr.length > 0) {
var rnd:int = Math.random() * arr.length;
tmp.push(arr.splice(rnd,1));
}
return tmp;
}```

3. 820
posts
Registered User
For a deck of 52 cards, here's what I use:
PHP Code:
``` _root.cardcount=1; cards=new Array(); used=new Array(); for(c=1;c<53;c++){     newcard = Math.ceil(Math.random() * 52);     while(used[newcard]) {         newcard = Math.ceil(Math.random() * 52);     }     used[newcard]=true;     cards[c]=newcard; }  ```

4. Code:
```deck = new Array("1C", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "1D", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "1D", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "1H", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "1S", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS")
deck.sort(function () {return Math.round(Math.random());});
trace (deck);```

5. 13
posts
Registered User
Glos....my sox are officially rox'd. Thats exactly what I was looking for! Thanks

6. Nice bit of code glos, very simple.

7. rrh
207
posts
Registered User
The downside of glosrfc's method is that not all card orders are equally likely.

I use something more like kosko's example, because I know all results are equally likely.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•