Here you can find the source of resampleWithReplacement(final List l, final int n)
public static List resampleWithReplacement(final List l, final int n)
//package com.java2s; /**// www .j a v a 2s . c o m * Musite * Copyright (C) 2010 Digital Biology Laboratory, University Of Missouri * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Set; public class Main { /** * * @param N * @param n */ public static List<Integer> resampleWithReplacement(final int N, final int n) { return resampleWithReplacement(N, n, new Random()); } public static List<Integer> resampleWithReplacement(final int N, final int n, final Random rand) { return resampleWithReplacement(N, n, null, true, rand); } public static List<Integer> resampleWithReplacement(final int N, final int n, final Set<Integer> exclusiveIndicesFilter) { return resampleWithReplacement(N, n, exclusiveIndicesFilter, true); } public static List<Integer> resampleWithReplacement(final int N, final int n, final Set<Integer> indicesFilter, final boolean exclude) { return resampleWithReplacement(N, n, indicesFilter, exclude, new Random()); } public static List<Integer> resampleWithReplacement(final int N, final int n, Set<Integer> indicesFilter, boolean exclude, final Random rand) { if (rand == null) { throw new NullPointerException(); } if (N <= 0 || n <= 0) { throw new IllegalArgumentException(); } List<Integer> indices = new ArrayList<Integer>(n); for (int i = 0; i < n;) { int ix = rand.nextInt(N); if (indicesFilter != null && indicesFilter.contains(ix) == exclude) continue; indices.add(ix); i++; } return indices; } public static List resampleWithReplacement(final List l, final int n) { return resampleWithReplacement(l, n, new Random()); } public static List resampleWithReplacement(final List l, final int n, final Random rand) { return resampleWithReplacement(l, n, null, true, rand); } public static List resampleWithReplacement(final List l, final int n, Set<Integer> exclusiveIndicesFilter) { return resampleWithReplacement(l, n, exclusiveIndicesFilter, true); } public static List resampleWithReplacement(final List l, final int n, Set<Integer> indicesFilter, boolean exclude) { return resampleWithReplacement(l, n, indicesFilter, exclude, new Random()); } public static List resampleWithReplacement(final List l, final int n, Set<Integer> indicesFilter, boolean exclude, final Random rand) { if (l == null || rand == null) { throw new NullPointerException(); } int N = l.size(); if (N <= 0 || n <= 0) { throw new IllegalArgumentException(); } List ret = new ArrayList(n); for (int i = 0; i < n;) { int ix = rand.nextInt(N); if (indicesFilter != null && indicesFilter.contains(ix) == exclude) continue; ret.add(l.get(ix)); i++; } return ret; } }