Shuffling the deck of cards perfectly randomly.

Problem

Write the program to shuffle the deck of cards in statistically random order.


 Knuth shuffle 

Simple way to shuffle the card in statistically random order is to use Knuth shuffle algorithm.
Basically, it mimics the shuffling the deck of cards by human as follows:

1. Pick one card randomly from the deck and remove it from the deck of chards an place the card in one of table.
2. Pick another card randomly from the deck and place it on the top of another card.
3. Repeat the step 2 until there is no card left.

At the end of this process, there will be a new deck of cards randomly shuffle.
I is important to choose correctly random generation library. In this practice code, I used std::uniform_real_distribution and std::default_random_engine.

Here is the complete code running in O(N).


Practice statistics:

29:00 to write up the code and fix up the logical flaws and typos.


Retried the same question in Python. (May 28 2019)
Did not apply the swap as in Knuth shuffle algorithm.


Python solution




Practice statistics:

15:00: to write up the code. Had to know how to use random module.


Comments

Popular posts from this blog

Planting flowers with no adjacent flower plots

Stock price processing problem