Forums for the Business Analyst

 
  Modern Analyst Forums  Business and Sy...  General  Perfect Shuffle
Previous Previous
 
Next Next
New Post 11/16/2016 11:56 PM
User is offline CodeGroundOnlineTestingPlatform
5 posts
10th Level Poster


Perfect Shuffle 
Modified By Chris Adams  on 11/17/2016 11:13:52 AM)
Hi All,
I found the comparison between these two random shuffling algorithms to be interesting.
Consider two shuffling algorithms
SHUFFLE 1
shuffle(A[1 … n])   {
    for i = 1 to n   {
        // Find a random integer between 1 and n inclusive
        int rand= RANDOM(1,n);
        swap A[i] with A[rand];
    }
}
SHUFFLE 2
shuffle(A[1 … n])   {
    for i = 1 to n   {
        // Find a random integer between i and n inclusive
        int rand= RANDOM(i,n);
        swap A[i] with A[rand];
    }
}
How do these two shuffling algorithms compare against each other? 
Which of these two is a perfect shuffling algorithm?
Consider an array with distinct elements A[1 … n]
A perfectly unbiased shuffle algorithm would randomly shuffle all elements in the array such that after shuffling:
1.The probability that the shuffling operation would produce any particular permutation of the original array is the same for all permutations (i.e.) since there are n! permutations, the probability that the shuffle operation would produce any particular permutation is 1/n!
2.For any element e in the array and for any position j (1<= j <= n), the probability that the element would end up in position A[j] is 1/n
Simulating Shuffle 1 and Shuffle 2 clearly proves that Shuffle 1 is biased while Shuffle 2 is unbiased
Can we prove that Shuffle 2 will produce an unbiased shuffle in all cases?
For any element e, the probability that it will be shuffled into the first position
= probability that it is selected for swapping when i = 1
= 1/n
For any element e, the probability that it will be shuffled into the second position
= probability that it is NOT selected for the first position x probability that it is selected for swapping when i = 2
= (n-1)/n   x   1/(n-1)
= 1/n

For any element e, the probability that it will be shuffled into any particular position = 1/n

 

Was this useful?
 
Previous Previous
 
Next Next
  Modern Analyst Forums  Business and Sy...  General  Perfect Shuffle

Community Blog - Latest Posts

I have always loved cooking. I learned from my Grandma June and her kitchen was her sanctuary, a small, warm sunlit space filled with jars of spices, stacks of cookbooks, and the comforting smell of something always on the stove or baking in the oven. Grandma June was as great a cook as she was a teacher to me. She never followed a recipe “to...
Now, I know, I don’t know what kind of new intro BPO may require, but at least I can promise you upfront that I have something different to say about Business Process Outsourcing or simply BPO. What is it? This hasn't been a call center job or a sales job as we all think. It is a mammoth profile, and firms are grappling to ge...
Leveraging Blockchain Technology for Enhanced Data Security in Business Operations
In an age where data breaches and cyberattacks are becoming more frequent and sophisticated, businesses are searching for robust solutions to safeguard their data. Blockchain technology has emerged as a promising tool in this quest, offering a way to secure data with unparalleled reliability. Although initially known for powering cryptocurrencies l...

 






 

Copyright 2006-2025 by Modern Analyst Media LLC