The forums have permanently moved to 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.

Results 1 to 7 of 7

Thread: Help needed with a card shuffling function

  1. #1

    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
    swap = 2
    while(array1.length > 0){
    num = random(2);
    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);
    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. #2
    Registered User
    here is one of many methods to suffle, not the best...

    var arr:Array = new Array("jedan", 
    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;
    	return tmp;
    Everything is possible....

  3. #3
    For a deck of 52 cards, here's what I use:
    PHP Code:
    cards=new Array();
    used=new Array();
    newcard Math.ceil(Math.random() * 52);
    used[newcard]) {
    newcard Math.ceil(Math.random() * 52);

  4. #4
    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);
    2006-11 GlosRFC - Searching 8,168,684,336 brain cells

  5. #5 sox are officially rox'd. Thats exactly what I was looking for! Thanks

  6. #6
    Nice bit of code glos, very simple.

  7. #7
    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.

Thread Information

Users Browsing this Thread

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

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012