李顺利
随笔-50  评论-170  文章-0  trackbacks-0

今天为大家写个小程序。

工作中有遇到一些抽奖的活动,但是你懂得,抽奖物品的概率肯定不是一样,你会发现好的东西很难抽到,经常抽到一些垃圾的东西,嘿嘿,这就是本文要说的,我们要控制抽奖物品的概率。还有顺便说一句,网上这种小程序几乎没有,很多都是等概率的抽奖balabala…

需求很简单,为了更加形象,这里我们列一个表格来显示我们抽奖的物品和对应的概率(没有边框,大家凑合着看看吧,不想改造Octopress的样式了)

序号 物品名称 物品ID 抽奖概率
1 物品1 P1 0.2
2 物品2 P2 0.1
3 物品3 P3 0.4
4 物品4 P4 0.3
5 物品5 P5 0.0
6 物品6 P6 -0.1
7 物品7 P7 0.008

数据很简单,那么就直接看代码了

VO类,具体对应就是上面表格里的内容

(Gift.java)download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.usc.usc.lottery;

public class Gift {
    private int index;
    private String gitfId;
    private String giftName;
    private double probability;

    public Gift(int index, String gitfId, String giftName, double probability) {
        this.index = index;
        this.gitfId = gitfId;
        this.giftName = giftName;
        this.probability = probability;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getGitfId() {
        return gitfId;
    }

    public void setGitfId(String gitfId) {
        this.gitfId = gitfId;
    }

    public String getGiftName() {
        return giftName;
    }

    public void setGiftName(String giftName) {
        this.giftName = giftName;
    }

    public double getProbability() {
        return probability;
    }

    public void setProbability(double probability) {
        this.probability = probability;
    }

    @Override
    public String toString() {
        return "Gift [index=" + index + ", gitfId=" + gitfId + ", giftName=" + giftName + ", probability=" + probability + "]";
    }

}

工具类,真正的不同概率的抽奖就在这里

(LotteryUtil.java)download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.usc.usc.lottery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 不同概率抽奖工具包
 *
 * @author Shunli
 */
public class LotteryUtil {
    /**
     * 抽奖
     *
     * @param orignalRates
     *            原始的概率列表,保证顺序和实际物品对应
     * @return
     *         物品的索引
     */
    public static int lottery(List<Double> orignalRates) {
        if (orignalRates == null || orignalRates.isEmpty()) {
            return -1;
        }

        int size = orignalRates.size();

        // 计算总概率,这样可以保证不一定总概率是1
        double sumRate = 0d;
        for (double rate : orignalRates) {
            sumRate += rate;
        }

        // 计算每个物品在总概率的基础下的概率情况
        List<Double> sortOrignalRates = new ArrayList<Double>(size);
        Double tempSumRate = 0d;
        for (double rate : orignalRates) {
            tempSumRate += rate;
            sortOrignalRates.add(tempSumRate / sumRate);
        }

        // 根据区块值来获取抽取到的物品索引
        double nextDouble = Math.random();
        sortOrignalRates.add(nextDouble);
        Collections.sort(sortOrignalRates);

        return sortOrignalRates.indexOf(nextDouble);
    }
}

测试类,测试上面的抽奖是否成功,n次抽奖看抽奖结果

(LotteryTest.java)download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.usc.usc.lottery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 不同概率抽奖
 *
 * @author ShunLi
 */
public class LotteryTest {
    public static void main(String[] args) {
        List<Gift> gifts = new ArrayList<Gift>();
        // 序号==物品Id==物品名称==概率
        gifts.add(new Gift(1, "P1", "物品1", 0.2d));
        gifts.add(new Gift(2, "P2", "物品2", 0.2d));
        gifts.add(new Gift(3, "P3", "物品3", 0.4d));
        gifts.add(new Gift(4, "P4", "物品4", 0.3d));
        gifts.add(new Gift(5, "P5", "物品5", 0d));
        gifts.add(new Gift(6, "P6", "物品6", -0.1d));
        gifts.add(new Gift(7, "P7", "物品7", 0.008d));

        List<Double> orignalRates = new ArrayList<Double>(gifts.size());
        for (Gift gift : gifts) {
            double probability = gift.getProbability();
            if (probability < 0) {
                probability = 0;
            }
            orignalRates.add(probability);
        }

        // // test
        // for (int i = 0; i < 10000; i++) {
        // try {
        // Gift tuple = gifts.get(LotteryUtil.lottery(orignalRates));
        // System.out.println(tuple);
        // } catch (Exception e) {
        // System.out.println("lottery failed, please check it!");
        // }
        // }

        // statistics
        Map<Integer, Integer> count = new HashMap<Integer, Integer>();
        double num = 1000000;
        for (int i = 0; i < num; i++) {
            int orignalIndex = LotteryUtil.lottery(orignalRates);

            Integer value = count.get(orignalIndex);
            count.put(orignalIndex, value == null ? 1 : value + 1);
        }

        for (Entry<Integer, Integer> entry : count.entrySet()) {
            System.out.println(gifts.get(entry.getKey()) + ", count=" + entry.getValue() + ", probability=" + entry.getValue() / num);
        }
    }

}

结果

1
2
3
4
5
Gift [index=1, gitfId=P1, giftName=物品1, probability=0.2], count=199139, probability=0.199139
Gift [index=2, gitfId=P2, giftName=物品2, probability=0.1], count=99328, probability=0.099328
Gift [index=3, gitfId=P3, giftName=物品3, probability=0.4], count=396575, probability=0.396575
Gift [index=4, gitfId=P4, giftName=物品4, probability=0.3], count=296997, probability=0.296997
Gift [index=7, gitfId=P7, giftName=物品7, probability=0.0080], count=7961, probability=0.007961

不同概率的抽奖原理很简单 
就是把0到1的区间分块,而分块的依据就是物品占整个的比重,再根据随机数种子来产生0-1中间的某个数,来判断这个数是落在哪个区间上,而对应的就是抽到了那个物品。随机数理论上是概率均等的,产生的每个数理论上也应该概率均等,那么相应的区间所含数的多少就体现了抽奖物品概率的不同。(p.s. 当然数目是数不清楚的,具体抽象话了点)

这个实例的数据可以说明 
1. 概率可以是负数和0,当然实际上中应该不会(p.s. 正常情况下可能真的有0,比如抽个iphone5,当然是抽不到的了,这个时候,构建礼物(List gifts)的时候最好就不要加这个进去),还有可以把负数的处理放到抽奖工具类(LotteryUtil)中; 
2. 所有礼物加起来的概率可以不是1,可以认为这里的概率是一个权重;

小小分享了,倒是觉得大家可以自己先想想,如果你来写这样的小程序,如何来写,有没有其它的创意和想法?如果有什么建议或问题的话,可以通过微博 http://weibo.com/lishunli 联系到我,大家一起交流学习。

顺利
2012年10月17日



博客中的一些下载已经放到了百度云了,请根据需要下载。【点我去百度云下载】

最后弱弱地说一下,如果可以的话,转载请提供出处( ),谢谢。
posted on 2012-10-17 18:28 李顺利 阅读(10182) 评论(2)  编辑  收藏

评论:
# re: 不同概率的抽奖 2013-11-23 12:03 | dohkoos
// 计算每个物品在总概率的基础下的概率情况
List<Double> sortOrignalRates = new ArrayList<Double>(size);
Double tempSumRate = 0d;
for (double rate : orignalRates) {
tempSumRate += rate;
sortOrignalRates.add(tempSumRate / sumRate);
}
在上述的代码中sumRate值是固定的,tempSumRate值是逐渐增大的,所以sortOrgnalRates中的值已经是排好序了的。所以不需要再用下面的代码去排序选出索引。
// 根据区块值来获取抽取到的物品索引
double nextDouble = Math.random();
sortOrignalRates.add(nextDouble);
Collections.sort(sortOrignalRates);

return sortOrignalRates.indexOf(nextDouble);

可以改成
double nextDouble = Math.random();
Double tempSumRate = 0d;
int i;
for (i = 0; i < orignalRates.size(); i++) {
tempSumRate += rate;
if (nextDouble <= tempSumRate) {
break;
}
sortOrignalRates.add(tempSumRate / sumRate);
}
return i;  回复  更多评论
  
# re: 不同概率的抽奖 2014-04-09 16:17 | 李顺利
@dohkoos
不好意思,现在才看见,后面的排序之所以放在for后面是为了解决所有的概率加起来不一定是1.0(可能大于1),而Math.random()只能返回[0,1)的double数,所以修正下 double nextDouble = Math.random() * sumRate; 即可

当然排序后indexof可以用foreache循环在来一次,这里只是有点偷懒了,这里就是一个查找的问题了,方法很多,比如二分查找。
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: