我想生成从 0
到大小的指标 - 1
,均匀,但比指数不同在给定的排除
。
尺寸
约为100通常有1,2或3 已排除
指数。在排除
指数是独一无二的,没有排序。
在理想情况下,头会随着多个参数,也许是这样的:
INT getRandomIndex(随机兰特,诠释大小,INT ... I)
或者,如果该 ...
多参数是缓慢的(是吗?),我们可以通过简单的 INT []排除
数组或什么的。
怎么做快? getRandomIndex()
被称为数百万次。
静态INT getRandomIndex(随机兰特,诠释大小,整数...排除){
名单<整数GT; excludeList = Arrays.asList(不含);
INT编号;
做 {
数= rand.nextInt(大小);
}而(excludeList.contains(数));
返回数;
}
I would like to generate an index from 0
to size - 1
, uniformly, but different than any index in the given set excluded
.
size
is around 100. There are typically 1, 2 or 3 exluded
indices. Indices in excluded
are unique and not sorted.
Ideally, the header will be with multiple arguments, maybe something like this:
int getRandomIndex(Random rand, int size, int... i)
Or, if this ...
multi arguments are slow (are they?) we can pass simple int[] excluded
array or something.
How to do it fast? getRandomIndex()
is called millions of times.
static int getRandomIndex(Random rand, int size, Integer... excludes) {
List<Integer> excludeList = Arrays.asList(excludes);
int number;
do {
number = rand.nextInt(size);
} while (excludeList.contains(number));
return number;
}