生成随机指标不同,那么在一组给定的任何一个任何一个、指标、不同

2023-09-11 23:28:06 作者:折月煮酒゜

我想生成从 0 大小的指标 - 1 ,均匀,但比指数不同在给定的排除

尺寸约为100通常有1,2或3 已排除指数。在排除指数是独一无二的,没有排序。

在理想情况下,头会随着多个参数,也许是这样的:

  INT getRandomIndex(随机兰特,诠释大小,INT ... I)
 

或者,如果该 ... 多参数是缓慢的(是吗?),我们可以通过简单的 INT []排除数组或什么的。

怎么做快? getRandomIndex()被称为数百万次。

解决方案

 静态INT getRandomIndex(随机兰特,诠释大小,整数...排除){
    名单<整数GT; excludeList = Arrays.asList(不含);
    INT编号;
    做 {
        数= rand.nextInt(大小);
    }而(excludeList.contains(数));

    返回数;
}
 

给定一组数字,如何在excel上画它的正态分布曲线图

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;
}