CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    [RESOLVED] Randomly Sorting an Array

    Hi, I am trying to write a class that simulates a deck of cards and i am currently working on the shuffle method. I am using a char array to simulate the deck. I remember reading somewhere that there are already methods to randomly sort an array but i wanted to implement the shuffle with my own code. The char array i start with is in a ascending order so it looks like. [2,2,2,2 ... ... K,K,K,K,A,A,A,A]. I was wondering how this algorithm would work and whether others thought it was random enough.


    For each index of the array
    get a random number within the array bounds.
    switch the current char with the char at the random index.


    Would this be a good way to shuffle the array. It looks like I will be creating 52 random numbers in total and making 52 swaps total. It does not seem like alot of overhead. I have not tried to write the code yet i just wanted to get some opinions on the alg. and whether or not it would suit. I was also thinking I could run the algo twice on the array.

    Gerald

  2. #2
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: Randomly Sorting an Array

    Hey, i wrote the code and ran it. It seems to shuffle the deck pretty well. Ill post results. And i would still like to know whether this would be a good way to shuffle or if there is a more efficient way/ more random way.

    Code:
    2222
    3333
    4444
    5555
    6666
    7777
    8888              //Before
    9999
    0000
    JJJJ
    QQQQ
    KKKK
    AAAA
    ----------
    
    QJ80
    QK70
    53K4
    J56Q
    6J54
    8629
    7968           //After
    39KQ
    AK27
    2593
    4008
    43A2
    7AAJ
    ----------
    
    /*
     * This function Shuffles a Deck of cards.
     */
    void shuffle()
    {
        int temp=0;
        int random=0;
    
        // initialize a random seed.
        srand ( time(NULL) );
    
        for(int i=0;i<52;i++)
        {
            //Generate a random number for the swap.
            //  The random number refers to an index.
            random= rand() &#37; 51 ;
    
            //Swapping the chars at the index and the random index.
            temp=deck[i];
            deck[i]=deck[random];
            deck[random]=temp;
        }
    }

    Gerald

    This is a 1d array. I just have it printing results with 4 columns and 13 rows.

  3. #3
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: [RESOLVED] Randomly Sorting an Array

    I found another post about the same topic that covered most of my questions.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured