java实现一个抽奖概率类
来源:互联网 发布:域名注册商排名 编辑:程序博客网 时间:2024/05/20 15:57
原文地址:http://blog.csdn.net/codefunjava/article/details/44408555
在一些项目需求中,可能会遇到抽奖问题,如提供一系列奖品及获奖概率,要求根据概率返回每次抽到的奖品。以下是本人在实际项目中写的一个抽奖工具类,与大家共同分享:
该工具类的基本思想是,将抽奖概率分布到数轴上,如现有三个抽奖概率10、20、30,将三者依次添加到概率集合中,则构造的数轴为:0~10范围内表示概率10,10~30范围内表示概率为20,30~60范围内表示概率为30,数轴上的长度对应着相应的概率。由这种处理方式可知,概率总和并不需要等于1。该工具类的成功与否在于Random.nextDouble()能否等概率地生成0~1之间的任意一个数。对该抽奖工具进行测试,测试类如下:
- package com.lottery;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
-
- class Result{
- private int index;
- private int sumTime;
- private int time;
- private double probability;
- private double realProbability;
-
- public int getIndex() {
- return index;
- }
-
- public void setIndex(int index) {
- this.index = index;
- }
-
- public int getTime() {
- return time;
- }
-
- public void setTime(int time) {
- this.time = time;
- }
-
- public int getSumTime() {
- return sumTime;
- }
-
- public void setSumTime(int sumTime) {
- this.sumTime = sumTime;
- }
-
- public double getProbability() {
- return probability;
- }
-
- public double getRealProbability() {
- return realProbability;
- }
-
- public void setRealProbability(double realProbability) {
- this.realProbability = realProbability;
- }
-
- public Result(){
-
- }
-
- public Result(int index, int sumTime, int time, double realProbability) {
- this.setIndex(index);
- this.setTime(time);
- this.setSumTime(sumTime);
- this.setRealProbability(realProbability);
-
- }
-
- public String toString(){
- return "索引值:" + index + ",抽奖总数:" + sumTime + ",抽中次数:" + time + ",概率:"
- + realProbability + ",实际概率:" + (double)time/sumTime;
- }
- }
-
- public class TestLottery {
-
- static final int TIME = 100000;
-
- public static void iteratorMap(Map<Integer, Integer> map, List<Double> list){
- for(Entry<Integer, Integer> entry : map.entrySet()){
- int index = entry.getKey();
- int time = entry.getValue();
- Result result = new Result(index, TIME, time, list.get(index));
- System.out.println(result);
- }
- }
-
- public static void main(String[] args) {
-
- List<Double> list = new ArrayList<Double>();
- list.add(20d);
- list.add(80d);
- list.add(50d);
- list.add(30d);
- LotteryUtil ll = new LotteryUtil(list);
- double sumProbability = ll.getMaxElement();
-
- Map<Integer, Integer> map = new HashMap<Integer, Integer>();
- for(int i = 0; i < TIME; i++){
- int index = ll.randomColunmIndex();
- if(map.containsKey(index)){
- map.put(index, map.get(index) + 1);
- }else{
- map.put(index, 1);
- }
- }
- for(int i = 0; i < list.size(); i++){
- double probability = list.get(i) / sumProbability;
- list.set(i, probability);
- }
- iteratorMap(map, list);
-
- }
-
-
-
- }
运行结果:
由结果可知,抽奖100000时, 得到的实际概率基本与正式概率相当。
以下说明此类调用方式:
- public LotteryUtil(List<Double> list)
- 说明:构造方法,传入参数为一个概率集合
- public int randomColunmIndex()
- 功能:进行抽奖操作,返回List集合的索引下标,此下标对应的概率的奖品即为抽中的奖品
0 0