Community Sites
Create your own community website and start earning today !
It's Free !
 
Communities Members BookmarksPolls Fresher Jobs Funny Pictures MCA Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



math function asp.net


Posted Date: 24 Mar 2008    Resource Type: Articles/Knowledge Sharing    Category: Career Guidance

Posted By: ramya       Member Level: Gold
Rating:     Points: 5




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?

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

Next
' 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
Next
ShuffleDeck = Shuffle( deck, deckSize )
End Function
%>



<%
Randomize

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: " _
& str & "
    " & vbNewLine
    sh = Shuffle( ar, 4 )
    For i = 1 to 4
    Response.Write "
  1. " & sh(i) & vbNewLine
    Next

    Response.Write "

" & vbNewLine

Response.Write "Shuffling a 'deck' of 20 numbered cards.
" _
& "The cards were originally numbered from 1 to 20.

" & vbNewLine
sh = ShuffleDeck( 20 )
str = Mid( Join( sh, "," ), 2 )
Response.Write "The shuffled deck: " & str & "" & 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.






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Lionbridge Technologies Profile
Previous Resource: dot net interview Q&A part 3
Return to Discussion Resource Index
Post New Resource
Category: Career Guidance


Post resources and earn money!
 
Related Resources

Watch TV Channels



Contact Us    Editors    Privacy Policy    Terms Of Use   

SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.