Randomly generate m integers from an array of size n
Problem 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.
Solution This question depends on 2 things:
how random number generation works? Fisher yates algorithm or Knuth Shuffle? Now that we have fisher yates algorithm in our quiver, we can solve this problem :
let m be the number of elements to select for i = 1; i <= m; i++ pick a random number from 1 to n, call it j swap array and array (assuming 1 indexed arrays) n-- public static int chooseNFromM(int array, int m) { if (m > array.
[Read More]