Shuffle a deck of cards perfectly

Question Write a method to shuffle a deck of cards. It must be a perfect shuffle – in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. OR How can you shuffle an array in O(n) time and O(1) space? For example, if input is an array of (1, 2, 3, 4, 5), one of the output can be (5, 4, 3, 2, 1) or (4, 2, 3, 1, 5). [Read More]

Random number generation

Understanding how random number generation (RNG) works In java, lets say you wants to generate random number between i to j e.g. [ i,j ) excluding upper limit. One standard pattern for accomplishing this is: Min + (int)(Math.random() \* ((Max - Min) + 1)) The java Math library function Math.random() generates a double value in the range [0,1). Notice this range does not include the 1. In order to get a specific range of values first you need to multiply by the magnitude of the range of values you want covered. [Read More]