Java – NaN Quantitavity – Quant Trading, Statistical Learning, Coding and Brainstorming
Random Permutation 2 – Random sample m out of n
Filed under: C++, Java — weekendsunny @ 10:31 PM
int[] pickMRandomly(int[] array, int m) { int[] subset = new int[m]; for (int j = 0; j < m; j++) { int index = random(j, n); // random number between j and n int temp = array[index]; array[index] = array[j]; array[j] = temp; subset[j] = temp; } return subset; }
Random Permutation 1 – shuffle
Filed under: C++, Java — weekendsunny @ 10:29 PM
public static void shuffleArray(int[] array) { Random generator = new Random(); for (int j = 0; j < array.length; j++) { int index = generator.nextInt(array.length-j)+j; // random number between j and n int temp = array[index]; array[index] = array[j]; array[j] = temp; } }