3 Replies Latest reply on Dec 28, 2010 1:32 PM by kglad

# random card draws

There's something I'm trying to make but I just can't seem to figure it out. I want to make something that allows a person to build a deck of cards from multiple sets of cards. I also want a button that can be clicked to randomly choose a card from the created deck. For example: lets say there are four sets of thirteen cards (hearts, diamonds, spades, and clubs). The user can select which of the four sets will be included in the deck. Once the sets are chosen the user can click a button and one card will be chosen from the selected sets. What is the best way to handle something like this? Is there a tutorial that explains how to do something like this? I've done some searches but to be honest I have no idea what I'm supposed to be searching for, so I haven't found anything that's of much help.

• ###### 1. Re: random card draws

push whatever cards you want to randomize into an array and then pass that array to the shuffle function.  the array will then be randomized and you can select cards by referencing the first through whatever array element.

function shuffle(a:Array):Array {
var len:Number = a.length-1;
for (var ivar:Number = len; ivar>=0; ivar--) {
var p:Number = Math.floor(Math.random()*(ivar+1));
var t = a[ivar];
a[ivar] = a[p];
a[p] = t;
}
return a;
}

• ###### 2. Re: random card draws

Thanks. Looks like I'm going to have to learn a bit more about how arrays work.

• ###### 3. Re: random card draws

you're welcome.