@Test public void testSample() { RandomDataGenerator random = new RandomDataGenerator(new MersenneTwister(123)); List<Integer> arr = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); assertEquals(newHashSet(0, 2, 3, 5, 6, 8, 9, 11), newHashSet(RandomUtils.sample(arr, 8, random))); }
public static <T> List<T> sample(List<T> items, int m, RandomDataGenerator random) { Preconditions.checkArgument(0 <= m, "Sample size cannot be negative"); Preconditions.checkArgument(m <= items.size(), "Sample size cannot be greater than list size!"); final IntDoubleLinkedSet indices = new IntDoubleLinkedSet(); final List<T> res = new ArrayList<>(m); final int n = items.size(); for (int i = n - m; i < n; i++) { int pos = nextInt(0, i, random); if (indices.contains(pos)) { indices.add(i); res.add(items.get(i)); } else { indices.add(pos); res.add(items.get(pos)); } } return res; }
RandomUtils { public static <T> List<T> sample(List<T> items, int m, RandomDataGenerator random) { Preconditions.checkArgument(0 <= m, "Sample size cannot be negative"); Preconditions.checkArgument(m <= items.size(), "Sample size cannot be greater than list size!"); final IntDoubleLinkedSet indices = new IntDoubleLinkedSet(); final List<T> res = new ArrayList<>(m); final int n = items.size(); for (int i = n - m; i < n; i++) { int pos = nextInt(0, i, random); if (indices.contains(pos)) { indices.add(i); res.add(items.get(i)); } else { indices.add(pos); res.add(items.get(pos)); } } return res; } }
RandomUtils { public static <T> List<T> sample(List<T> items, int m, RandomDataGenerator random) { Preconditions.checkArgument(0 <= m, "Sample size cannot be negative"); Preconditions.checkArgument(m <= items.size(), "Sample size cannot be greater than list size!"); final IntDoubleLinkedSet indices = new IntDoubleLinkedSet(); final List<T> res = new ArrayList<>(m); final int n = items.size(); for (int i = n - m; i < n; i++) { int pos = nextInt(0, i, random); if (indices.contains(pos)) { indices.add(i); res.add(items.get(i)); } else { indices.add(pos); res.add(items.get(pos)); } } return res; }  }
RandomUtils { public static <T> List<T> sample(List<T> items, int m, RandomDataGenerator random) { Preconditions.checkArgument(0 <= m, "Sample size cannot be negative"); Preconditions.checkArgument(m <= items.size(), "Sample size cannot be greater than list size!"); final IntDoubleLinkedSet indices = new IntDoubleLinkedSet(); final List<T> res = new ArrayList<>(m); final int n = items.size(); for (int i = n - m; i < n; i++) { int pos = nextInt(0, i, random); if (indices.contains(pos)) { indices.add(i); res.add(items.get(i)); } else { indices.add(pos); res.add(items.get(pos)); } } return res; }  static List<T> sample(List<T> items, int m, RandomDataGenerator random); static List<T> sampleMore(List<T> items, int m, RandomDataGenerator random); static List<T> sample(List<T> items, int m, Set<T> forbidden, RandomDataGenerator random); static List<T> sampleWithReplacement(List<T> items, int m, RandomDataGenerator random); static int[] shuffleInts(int[] ar, RandomDataGenerator random); static List<T> shuffle(List<T> items, RandomDataGenerator random); static List<T> randomSampleViaShuffle(Iterable<T> items, int sampleSize, RandomDataGenerator random); static int nextInt(int lower, int upper, RandomDataGenerator random); static double nextUniform(double lower, double upper, RandomDataGenerator random); static T pickRandom(List<T> items, RandomDataGenerator random); static int pickRandom(int[] arr, RandomDataGenerator random); static T pickRandom(T[] arr, RandomDataGenerator random); static double[] randomDoubleVector(int n, double minValue, double maxValue, RandomDataGenerator random); static float[] nextFloatVector(int n, float minValue, float maxValue, RandomDataGenerator random); static int sampleAccordingToDistribution(RandomDataGenerator random, double[] probabilities); }
RandomUtils { public static <T> List<T> sample(List<T> items, int m, RandomDataGenerator random) { Preconditions.checkArgument(0 <= m, "Sample size cannot be negative"); Preconditions.checkArgument(m <= items.size(), "Sample size cannot be greater than list size!"); final IntDoubleLinkedSet indices = new IntDoubleLinkedSet(); final List<T> res = new ArrayList<>(m); final int n = items.size(); for (int i = n - m; i < n; i++) { int pos = nextInt(0, i, random); if (indices.contains(pos)) { indices.add(i); res.add(items.get(i)); } else { indices.add(pos); res.add(items.get(pos)); } } return res; }  static List<T> sample(List<T> items, int m, RandomDataGenerator random); static List<T> sampleMore(List<T> items, int m, RandomDataGenerator random); static List<T> sample(List<T> items, int m, Set<T> forbidden, RandomDataGenerator random); static List<T> sampleWithReplacement(List<T> items, int m, RandomDataGenerator random); static int[] shuffleInts(int[] ar, RandomDataGenerator random); static List<T> shuffle(List<T> items, RandomDataGenerator random); static List<T> randomSampleViaShuffle(Iterable<T> items, int sampleSize, RandomDataGenerator random); static int nextInt(int lower, int upper, RandomDataGenerator random); static double nextUniform(double lower, double upper, RandomDataGenerator random); static T pickRandom(List<T> items, RandomDataGenerator random); static int pickRandom(int[] arr, RandomDataGenerator random); static T pickRandom(T[] arr, RandomDataGenerator random); static double[] randomDoubleVector(int n, double minValue, double maxValue, RandomDataGenerator random); static float[] nextFloatVector(int n, float minValue, float maxValue, RandomDataGenerator random); static int sampleAccordingToDistribution(RandomDataGenerator random, double[] probabilities);  }