Results 1 to 8 of 8

Thread: Sorting & Randomisation Of Array

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Sorting & Randomisation Of Array

    Hi all! This ones for anyone whos bored or bloody good with algorithms!

    Can anyone guide me on how to realistically shuffle a pack of cards?
    i.e. at the moment I have a 1 column array representing a card deck,
    with the element entries being Herts1 through to SpadeKing, what's the
    speediest / most efficient way best way to go about randomising these
    elements please - I've realised that on a normal shuffle, not all the cards
    are randomised & some stay together next to each other,
    so I'm not using the rnd functions at the moment.

    Thanks
    Last edited by alex_read; Aug 17th, 2002 at 04:11 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    here is the VB language version. It is very fast and very effective, especially around 100000 iterations. Experiment until you are satisfied with that.

    VB Code:
    1. Type Card
    2.      'card stuff goes here
    3. End Type
    4.  
    5. Public Deck(51) as Card
    6.  
    7. Sub InitDeck()
    8.      'assign all 52 cards.
    9. End Sub
    10.  
    11. Sub ShuffleDeck(Iterations as Long)
    12.      Dim lCounter1 as Long, lIndex1 as Long, lIndex2 as Long, tmpBuff as Card
    13.      For lCounter1 = 1 To Iterations Step 1
    14.           lIndex1 = Int(Rnd*52)
    15.           tmpBuff = Deck(lIndex1)
    16.           lIndex2 = Int(Rnd*52)
    17.           'get indexes and backup copy.
    18.           Deck(lIndex1) = Deck(lIndex2)
    19.           Deck(lIndex2) = tmpBuff
    20.           'swap cards
    21.      Next lCounter1
    22. End Sub

  3. #3
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    i don't 100% understand the code but from my POI the code takes two random integers and swap the cards in these two positions, and does it Iteration times.

    now, this does not guarentee every number to be picked and therefore moved with a rather small Iteration (say 52) --- although more likely than not. so i would think that when the program starts the cards are in strict increasing order, like 2-3-4-5-...-J-K-A, you could set Iteration to a rather large number.

    I have an alternate method that does guarentee a random sequence of cards. simpily treat it as a hash table. i don't know if you are familiar with the idea or not. instead of real shuffling, you re-generate the cards every time.

    so assume you have a array Deck[] whose values are set to 0:

    int c=Rnd();

    for (int i=0;i<52;i++){
    for (int j=(c*CardValue)%52;Deck[j]!=0;j=(j+1)%52);
    Deck[i]=CardValue;
    }

    now even though this method has a complexity of O(n2) but its average case is only O(n)-O(nlogn) -- which is what hash table is all about, fast look up.
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  4. #4
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    that's right bugz.

    [defending_****ty_code]
    Your hash table is very nice. Yes, it works better with more iterations. This is actually intentional, as the more times you shuffle the more random it gets. Basically all my code does is cut a deck a certain number of times. And it is actually more random than you might think.
    [/defending_****ty_code]

    hash table is probably better, though

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Alex - this is how to shuffle a deck - re-written from ancient code

    Code:
    Public cards(52)  ' or whatever array you use for your deck
    limit =52
    Loop While limit > 1
      x=Rnd() * limit  + 1
      Swap x,limit
      limit = limit - 1
    Loop
    
    Sub Swap(i as integer, j as integer)
        Dim tmp
        tmp = cards(j)
        cards(j)=cards(i)
        cards(i)=tmp
    End Sub

  6. #6
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    very nice code jim.. the reason I say this is because it's what I posted

  7. #7

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Thanks for the suggestions guys, much appriciated - helped me out a lot!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Snakey --

    Except what I posted is at least 15 years older than you are -
    it's from Knuth's 'Art of Computer Programming' 1968 - the algorithm was published in 1957 - I think.


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