@Test public void test() { RandomSampler<String> s = new RandomSampler<String>(); List<String> data = new ArrayList<String>(); for (int i = 0; i < 1000; i++) { data.add(String.valueOf(i)); } List<String> result = s.sample(data, 50); System.out.println(result); assertEquals(50, result.size()); }
public List<T> sample(List<T> data, int sampleNumber) { if (data == null) { throw new IllegalArgumentException("Input list is null"); } if (data.size() < sampleNumber) { return data; } List<T> result = new ArrayList<T>(sampleNumber); int n = data.size(); for (int i = 0; i < n; i++) { if (i < sampleNumber) { result.add(data.get(i)); } else { int j = rdm.nextInt(i); if (j < sampleNumber) { result.set(j, data.get(i)); } } } return result; }
RandomSampler { public List<T> sample(List<T> data, int sampleNumber) { if (data == null) { throw new IllegalArgumentException("Input list is null"); } if (data.size() < sampleNumber) { return data; } List<T> result = new ArrayList<T>(sampleNumber); int n = data.size(); for (int i = 0; i < n; i++) { if (i < sampleNumber) { result.add(data.get(i)); } else { int j = rdm.nextInt(i); if (j < sampleNumber) { result.set(j, data.get(i)); } } } return result; } }
RandomSampler { public List<T> sample(List<T> data, int sampleNumber) { if (data == null) { throw new IllegalArgumentException("Input list is null"); } if (data.size() < sampleNumber) { return data; } List<T> result = new ArrayList<T>(sampleNumber); int n = data.size(); for (int i = 0; i < n; i++) { if (i < sampleNumber) { result.add(data.get(i)); } else { int j = rdm.nextInt(i); if (j < sampleNumber) { result.set(j, data.get(i)); } } } return result; }  }
RandomSampler { public List<T> sample(List<T> data, int sampleNumber) { if (data == null) { throw new IllegalArgumentException("Input list is null"); } if (data.size() < sampleNumber) { return data; } List<T> result = new ArrayList<T>(sampleNumber); int n = data.size(); for (int i = 0; i < n; i++) { if (i < sampleNumber) { result.add(data.get(i)); } else { int j = rdm.nextInt(i); if (j < sampleNumber) { result.set(j, data.get(i)); } } } return result; }  List<T> sample(List<T> data, int sampleNumber); }
RandomSampler { public List<T> sample(List<T> data, int sampleNumber) { if (data == null) { throw new IllegalArgumentException("Input list is null"); } if (data.size() < sampleNumber) { return data; } List<T> result = new ArrayList<T>(sampleNumber); int n = data.size(); for (int i = 0; i < n; i++) { if (i < sampleNumber) { result.add(data.get(i)); } else { int j = rdm.nextInt(i); if (j < sampleNumber) { result.set(j, data.get(i)); } } } return result; }  List<T> sample(List<T> data, int sampleNumber);  }