Random value from enum with probability

前端 未结 7 983
既然无缘
既然无缘 2020-12-20 21:40

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

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 22:12

    import java.util.*;
    enum R {
        a(.1),b(.2),c(.3),d(.4);
        R(final double p) {
            this.p=p;
        }
        private static void init() {
            sums=new double[values().length+1];
            sums[0]=0;
            for(int i=0;i bins=new EnumMap(R.class);
            for(R r:R.values())
                bins.put(r,0);
            final int n=1000000;
            for(int i=0;i

提交回复
热议问题