来自枚举的随机值具有概率 [英] Random value from enum with probability

查看:126
本文介绍了来自枚举的随机值具有概率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举,我想随机选择一个值,但不是真正随机的。我希望有些价值观不太可能被选中。这是我到目前为止...

 私有枚举类型{
TYPE_A,TYPE_B,TYPE_C,TYPE_D, TYPE_E;

private static final列表< Type> VALUES =
Collections.unmodifiableList(Arrays.asList(values()));
private static final int SIZE = VALUES.size();
private static final Random RANDOM = new Random();

public static Type randomType(){
return VALUES.get(RANDOM.nextInt(SIZE));
}
}

有没有一种有效的方法来将概率分配给这些值?



这里

解决方案

几种方法来做,其中之一,类似于你的方法

 私有枚举类型{
TYPE_A(10 / * 10 - 此类型的重量* /),TYPE_B(1) ,TYPE_C(5),TYPE_D(20),TYPE_E(7);

private int weight;

private Type(int weight){
this.weight = weight;
}

private int getWeight(){
return weight;
}


private static final列表< Type> VALUES =
Collections.unmodifiableList(Arrays.asList(values()));

private int summWeigts(){
int summ = 0;
foreach(类型值:VALUES)
summ + = value.getWeight();
return summ;
}
private static final int SIZE = summWeigts();
private static final Random RANDOM = new Random();

public static类型randomType(){
int randomNum = RANDOM.nextInt(SIZE);
int currentWeightSumm = 0;
(Type currentValue:VALUES){
if(randomNum> currentWeightSumm&&
randomNum< =(currentWeightSumm + currentValue.getWeight()){
break;
}
currentWeightSumm + = currentValue.getWeight();
}

返回currentValue.get();
}
}


I have an enum that I would like to randomly select a value from, but not truly random. I would like some of the values to be less likely of being selected so far. Here is what I have so far...

private enum Type{
        TYPE_A, TYPE_B, TYPE_C, TYPE_D, TYPE_E;

        private static final List<Type> VALUES =
            Collections.unmodifiableList(Arrays.asList(values()));
          private static final int SIZE = VALUES.size();
          private static final Random RANDOM = new Random();

          public static Type randomType()  {
            return VALUES.get(RANDOM.nextInt(SIZE));
          }
    }

Is there an efficient way of assigning probabilities to each of these values?

Code found from here

解决方案

several ways to do it, one of them, similar to your approach

private enum Type{
    TYPE_A(10 /*10 - weight of this type*/), TYPE_B(1), TYPE_C(5), TYPE_D(20), TYPE_E(7);

private int weight;

private Type(int weight) {
    this.weight = weight;
}

private int getWeight() {
    return weight;
}


    private static final List<Type> VALUES =
        Collections.unmodifiableList(Arrays.asList(values()));

    private int summWeigts() {
       int summ = 0;
       foreach(Type value: VALUES) 
          summ += value.getWeight();
       return summ;
    }
    private static final int SIZE = summWeigts();
    private static final Random RANDOM = new Random();

    public static Type randomType()  {
        int randomNum = RANDOM.nextInt(SIZE);
        int currentWeightSumm = 0;
        for(Type currentValue: VALUES) {
           if (randomNum > currentWeightSumm && 
               randomNum <= (currentWeightSumm + currentValue.getWeight()) {
             break;
           }
           currentWeightSumm += currentValue.getWeight();
        }

        return currentValue.get();
    }
}

这篇关于来自枚举的随机值具有概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆