抽奖

Posted on 2012-11-09 16:03 志成中国 阅读(466) 评论(2)  编辑  收藏

今天为大家写个小程序。

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

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

序号物品名称物品ID抽奖概率
1物品1P10.2
2物品2P20.1
3物品3P30.4
4物品4P40.3
5物品5P50.0
6物品6P6-0.1
7物品7P70.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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 
package org.usc.usc.lottery;  import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random;  /**  * 不同概率抽奖工具包  *  * @author Shunli  */ public class LotteryUtil {     private static final Random random = new Random();      /**      * 抽奖      *      * @param orignalRates      *            原始的概率列表,保证顺序和实际物品对应      * @return      *         物品的索引      */     public static int lottery(List<Double> orignalRates) {         if (orignalRates == null || orignalRates.isEmpty()) {             return -1;         }          int size = orignalRates.size();          List<Double> sortOrignalRates = new ArrayList<Double>(size);         sortOrignalRates.addAll(orignalRates);         Collections.sort(sortOrignalRates);          List<Double> rates = new ArrayList<Double>(size);         // 计算总概率,这样可以保证不一定总概率是1         double sumRate = 0d;         for (double rate : sortOrignalRates) {             sumRate += rate;         }          // 计算每个物品在总概率的基础下的概率情况         Double tempSumRate = 0d;         for (double rate : sortOrignalRates) {             tempSumRate += rate;             rates.add(tempSumRate / sumRate);         }          double result = rates.get(0);         double nextDouble = random.nextDouble();          for (double rate : rates) {             if (nextDouble >= rate) {                 continue;             }             result = rate;             break;         }          // 根据区块值来获取抽取到的物品索引         int index = rates.indexOf(result);         int orignalIndex = orignalRates.indexOf(sortOrignalRates.get(index));          return orignalIndex;     } } 

测试类,测试上面的抽奖是否成功,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.1d));         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 联系到我,大家一起交流学习。

Feedback

# re: 抽奖[未登录]  回复  更多评论   

2012-11-17 10:48 by 小码哥
我们项目也是有摇奖模块,我也是用区间搞定的,

# re: 抽奖[未登录]  回复  更多评论   

2012-11-17 11:11 by 小码哥
兄台,你的算法有问题哟。

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


网站导航:
 

posts - 9, comments - 5, trackbacks - 0, articles - 0

Copyright © 志成中国