Your source for ASP-related frequently asked questions and answers! : ASP FAQS : Math Functions

Question: How can I shuffle a deck of cards or similar set of sequential numbers? How can I reorder a list of things in random order?

Answer: Think about one way you could do this with a physical deck of cards.

You could put the cards in order (not necessary, but in theory it shouldn't matter). Then you could randomly pick any card from the ones in the deck and either turn it over (to display it immediately) or move it to a second pile (to create a shuffled deck).

If you were really good about picking one card from the remaining ones in a truly random manner, the resulting output (display or deck) would be in truly random order.

Essentially, that's what we will do in the code that is given below.

The only hard part is replicating the physical removal of the card from the deck, so that the deck gets smaller and smaller. We could simply do this using an array and "sliding" all the cards "above" the chosen one "down" by one notch. This works. But it can be *very* slow!

So let's think of a slight modification of that physical scheme: Instead of just picking a random card from the deck, let's pull out the chosen card and--at the same time--take the card on the top of the deck and shove it there in place of the picked card. Does that change anything, really? Of course not! With good picking of random numbers, you get good random results.

And so, on to the code:

' This function will DEAL the "needed"
' number of values from the given "inArray"
' If the value for "needed" matches the
' upper bound of the "inArray", then the
' entire "inArray" is dealt out.
' NOTE: As written, this code never uses
' or touches element zero of the inArray
' and puts no value in element zero of the
' outArray. (Obviously, easy to change.)
Function Shuffle( inArray, needed )
    ' find out how many input elements there are...
    incnt = UBound( inArray )
    ' then create the output array to be the size
    ' requested via the "needed" argument
    dim outArray
    redim outArray( needed )

    ' now we will select the number of values
    ' specified as "needed"...
    For i = 1 To needed
        ' choose a random number from 1 to our
        ' current input array usage size...
        choose = Int( incnt * Rnd(1) ) + 1

        ' put that chosen element into the next
        ' slot in the output array...
        outArray( i ) = inArray( choose )
        ' here's the tricky part: Since we just
        ' used the "choose" element, we don't need
        ' it any more...we replace it with the last
        ' element of the in-use part of the array!
        inArray( choose ) = inArray( incnt )
        ' and then we (effectively) shrink the array!
        ' Next time through the loop, there will be
        ' one fewer elements in the array to choose
        ' from...because we have (effectively) deleted
        ' the one just chosen!
        incnt = incnt - 1

    ' return the shuffled output
    Shuffle = outArray
End Function

' This is just a convenience function
' If you need *all* the "cards" in a deck of a given
' size shuffled, and the "name" of a card can just be
' its numeric position in the unshuffled deck, then
' just call ShuffleDeck, passing the size of the deck
' to be shuffled.
Function ShuffleDeck( deckSize )
    Dim i, deck()
    ReDim deck( deckSize )
    For i = 1 To deckSize
        deck(i) = i
    ShuffleDeck = Shuffle( deck, deckSize )
End Function    



ar = Array(0,"you","can","put","anything","in","the","array","of","course")
str = Mid( Join(ar," "), 2 )
Response.Write "Picking 4 words from this list: <STRONG>" _
             & str & "</STRONG><OL>" & vbNewLine
sh = Shuffle( ar, 4 )
For i = 1 to 4
    Response.Write "<LI>" & sh(i) & vbNewLine

Response.Write "</OL><P> <P>" & vbNewLine

Response.Write "Shuffling a 'deck' of 20 numbered cards.<BR>" _
            & "The cards were originally numbered from 1 to 20.<P>" & vbNewLine
sh = ShuffleDeck( 20 )
str = Mid( Join( sh, "," ), 2 )
Response.Write "The shuffled deck: <STRONG>" & str & "</STRONG>" & vbNewLine


I have included a pair of demonstrations of the code. First, by picking four words from a list (array) of words. Second, I use the "auxiliary" function that I show there to generate and shuffle a "deck" of 20 cards.

Please do try these on your own machine(s)! Then adapt the functions as you will for your own purposes.

Happy programming!

FAQ posted by Bill Wilkinson at 1/17/2001 9:44:20 PM to the Math Functions category. This FAQ has been viewed 46,259 times.

Copyright 2018 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers