Category Archives: Probability

Random number generator from a set

Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. C++: 01 int randm(int m) 02 { 03     return rand()%m; 04 } 05  06 void randomsubset(int *A, … Continue reading

Posted in Probability | Leave a comment

Shuffle cards

Write a method to shuffle a deck of cards. It must be a perfect shuffle–in other words, each of the 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which … Continue reading

Posted in Probability | Leave a comment

Random number generation

Implement a method rand7() given rand5(). That is, given a method that generates a random number between 0 and 4(inclusive), write a method that generates a random number between 0 and 6(inclusive). C++: 01 int rand7() 02 { 03     int t=0; … Continue reading

Posted in Probability | Leave a comment

Select record uniformly

You need read a lot of records and you don’t know how many records here before you complete it. After you read all those records, you need select one record randomly. In another words, every record has same chance to … Continue reading

Posted in Number trick, Probability | Leave a comment