轮盘赌java算例

标签:#轮盘赌# 时间:2018/03/21 10:42:45 作者:十七岁的雨季

博客原文地址为:
http://blog.csdn.net/qy20115549/article/details/79635945

轮盘赌介绍


这里写图片描述

算法实现

package test;


public class Test {
    public static double[] multiPros;
    public static void main(String[] args) {
        multiPros = new double[4];
        multiPros[0] = 0.14;
        multiPros[1] = 0.49;
        multiPros[2] = 0.06;
        multiPros[3] = 0.31;
        for (int i = 0; i < 10; i++) {
            int k = nextDiscrete(multiPros);
            System.out.println(k);
        }

    }
     public static int nextDiscrete(double[] probs)
        {
            double sum = 0.0;
            for (int i = 0; i < probs.length; i++)
                sum += probs[i];

            double r = Math.random()  * sum;
            sum = 0.0;
            for (int i = 0; i < probs.length; i++) {
                sum += probs[i];
                if (sum > r)
                    return i;
            }
            return probs.length - 1;
        }
}

结果展示

这里写图片描述