Here is how you could answer it. He uses the fact that
- Assuming you use a shuffle to get an O (n) replacement for the βcards,β the module decreases with the shuffle. that is, start with
int[] all values ββand shuffle it like Collections.shuffle (). - you have more randomness than you need if you call getrnd50 () twice, for example, when you have less than 50 values ββleft to exchange.
EDIT: for those who are not familiar with how shuffling works, I added code for shuffling
import java.util.*; import java.lang.*; class Main { public static void main(String... args) { int samples = 100; // all the numbers [1, 100] int[] nums = new int[samples]; for (int i = 0; i < samples; i++) nums[i] = i + 1; for (int i = samples - 1; i > 0; i--) { int swapWith = nextInt(i + 1); // swap nums[i] and nums[swapWith] if (swapWith == i) continue; int tmp = nums[swapWith]; nums[swapWith] = nums[i]; nums[i] = tmp; } System.out.println("calls/sample " + (double) calls / samples); System.out.println(Arrays.toString(nums)); int[] count49 = new int[49]; for (int i = 0; i < 49 * 10000; i++) count49[nextInt(49) - 1]++; int[] count54 = new int[54]; for (int i = 0; i < 54 * 10000; i++) count54[nextInt(54) - 1]++; System.out.println("Histogram check (49): " + Arrays.toString(count49)); System.out.println("Histogram check (54): " + Arrays.toString(count54)); } // keep track of the range of values. static int maxRandom = 1; // some random value [0, maxRandom) static int rand100 = 0; static int nextInt(int n) { while (maxRandom < 10 * n * n) { maxRandom *= 50; rand100 = rand100 * 50 + getrnd50() - 1; } int ret = rand100 % n; maxRandom = (maxRandom + n - 1) / n; rand100 /= n; return ret + 1; } static final Random rand = new Random(); static int calls = 0; static int getrnd50() { calls++; return (1 + rand.nextInt(50)); } }
prints
calls / sample 0.94
[1, 37, 4, 98, 76, 53, 26, 55, 9, 78, 57, 58, 47, 12, 44, 25, 82, 2, 42, 30, 88, 81, 64, 99, 16 , 28, 34, 29, 51, 36, 13, 94, 80, 66, 19, 38, 20, 8, 40, 89, 72, 56, 75, 96, 35, 100, 95, 74, 69, 11 , 31, 86, 92, 6, 27, 22, 70, 63, 32, 93, 84, 71, 15, 23, 5, 14, 62, 49, 43, 87, 65, 83, 33, 45, 52 , 39, 91, 60, 73, 68, 24, 97, 46, 50, 18, 79, 48, 77, 67, 59, 10, 7, 54, 90, 85, 21, 61, 41, 3]
Histogram check (49): [10117, 10158, 10059, 10188, 10338, 9959, 10313, 10278, 10166, 9828, 10105, 10159, 10250, 10152, 9949, 9855, 10026, 10040, 9982, 10112, 10021, 10082 , 10029, 10052, 9996, 10057, 9849, 9990, 9914, 9835, 10029, 9738, 9953, 9828, 9896, 9931, 9995, 10034, 10067, 9745, 9873, 9903, 9913, 9841, 9823, 9859, 9941 , 10007, 9765]
Histogram check (54): [10124, 10251, 10071, 10020, 10196, 10170, 10123, 10096, 9966, 10225, 10262, 10036, 10029, 9862, 9994, 9960, 10070, 10127, 10021, 10166, 10077, 9983 , 10118, 10163, 9986, 9988, 10008, 9965, 9967, 9950, 9965, 9870, 10172, 9952, 9972, 9828, 9754, 10152, 9943, 9996, 9779, 10014, 9937, 9708, 9978, 9894, 9803 , 9904, 9915, 9927, 10000, 9838]
In this case, 100 numbers need less than 100 getrnd50 calls
If you have 1000 shuffle values
calls/sample 1.509