xxl.mathematica.random.RandomSample Maven / Gradle / Ivy
package xxl.mathematica.random;
import io.vavr.control.Try;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Function;
/**
* 伪随机采样
*/
public class RandomSample {
/**
* 不重复随机采样
*
* @param list
* @param n
* @param
* @return
*/
public static List randomSample(List list, final int n) {
return Try.ofCallable(new Callable>() {
@Override
public List call() throws Exception {
List rest = new ArrayList<>(list);
int num = n;
if (num > list.size()) {
num = list.size();
}
return io.vavr.collection.List.range(0, num)
.map(new Function() {
@Override
public T apply(Integer integer) {
return rest.remove(RandomInteger.randomInteger(rest.size()));
}
}).asJava();
}
}).get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy