Post
by Sailing_Away » Tue Jul 31, 2012 10:04 am
I'm a database administrator with a background in programming. Back in college, I not only took classes in programming but also stats and discrete math.
A complete pre-requisite is a randomizing function where you provide the number of possible choices (n), and this function has been thoroughly tested so you know over a very large sample, every possible result has a 1:n chance of being selected.
For the game's deck (could be 52 cards for hearts or spades or a smaller for euchre as appropriate), I would build an array representing every card (order unimportant - just you need to define one). I'll assume this is a 52 card deck for this explanation, and say I define hearts A, 2, 3..., J, Q, K, then diamonds, then clubs, then spades all the same order.
For the shuffling, I would pass n=52 into the function and it gives me a random number 1-52 (all with equal chance of being selected). The card that represents is card #1 in the shuffled deck, and I pop that one out of the array and move the higher array values down 1. Now I have 1 card in my shuffled deck and 51 cards in my array.
Now I repeat, passing n=51 into the function, and it gives me a random number from 1-51 where every possible response has a 1 in 51 chance of being selected. I pop that card out into the 2nd position of my shuffled deck, move the higher array occurrences down, and repeat until n=1 when I move the last card into the last position of my shuffled deck.
I agree with the comment that sometimes you will have the condition where you cannot believe the randomness was very random, but if you allow true randomization, you allow for all possible combinations and permutations including those that blow your socks off.
-Brian
"Come Sail Away with me...."