LotteryTool.java 1.11 KB
package com.cjs.site.util.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LotteryTool {
    private static Random random = new Random();

    /**
     * 抽奖
     */
    public static int lottery(List<Integer> orignalRates) {
        Integer weightSum = 100;
        Integer n = random.nextInt(weightSum); // n in [0, weightSum)  
        Integer m = 0;
        Integer num = 0;
        for (Integer wc : orignalRates) {
            if (m <= n && n < m + wc) {
                num = wc;
                break;
            }
            m += wc;
        }
        return num;
    }

    public static void main(String[] args) {
        List<Integer> orignalRates = new ArrayList<Integer>();
        orignalRates.add(10);
        orignalRates.add(90);
        int sum30 = 0;
        int sum70 = 0;
        for (int i = 1; i < 100; i++) {
            int a = lottery(orignalRates);
            if (10 == a) {
                sum30++;
            }
            if (90 == a) {
                sum70++;
            }
        }
        System.out.println(sum30 + "的次数=====" + sum70);
    }
}