查看
2015-06-01 22:30
363 查看
http://www.cnblogs.com/mycoding/archive/2011/07/07/2099878.html
工作中有遇到一些抽奖的活动,但是你懂得,抽奖物品的概率肯定不是一样,你会发现好的东西很难抽到,经常抽到一些垃圾的东西,嘿嘿,这就是本文要说的,我们要控制抽奖物品的概率。还有顺便说一句,网上这种小程序几乎没有,很多都是等概率的抽奖balabala…
需求很简单,为了更加形象,这里我们列一个表格来显示我们抽奖的物品和对应的概率(没有边框,大家凑合着看看吧,不想改造Octopress的样式了)
数据很简单,那么就直接看代码了
VO类,具体对应就是上面表格里的内容
(Gift.java)download
工具类,真正的不同概率的抽奖就在这里
(LotteryUtil.java)download
测试类,测试上面的抽奖是否成功,n次抽奖看抽奖结果
(LotteryTest.java)download
结果
不同概率的抽奖原理很简单
就是把0到1的区间分块,而分块的依据就是物品占整个的比重,再根据随机数种子来产生0-1中间的某个数,来判断这个数是落在哪个区间上,而对应的就是抽到了那个物品。随机数理论上是概率均等的,产生的每个数理论上也应该概率均等,那么相应的区间所含数的多少就体现了抽奖物品概率的不同。(p.s. 当然数目是数不清楚的,具体抽象话了点)
这个实例的数据可以说明
1. 概率可以是负数和0,当然实际上中应该不会(p.s. 正常情况下可能真的有0,比如抽个iphone5,当然是抽不到的了,这个时候,构建礼物(List gifts)的时候最好就不要加这个进去),还有可以把负数的处理放到抽奖工具类(LotteryUtil)中;
2. 所有礼物加起来的概率可以不是1,可以认为这里的概率是一个权重;
小小分享了,倒是觉得大家可以自己先想想,如果你来写这样的小程序,如何来写,有没有其它的创意和想法?如果有什么建议或问题的话,可以通过微博 http://weibo.com/lishunli 联系到我,大家一起交流学习。
// 计算每个物品在总概率的基础下的概率情况
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;
-------------------------------------------
工作中有遇到一些抽奖的活动,但是你懂得,抽奖物品的概率肯定不是一样,你会发现好的东西很难抽到,经常抽到一些垃圾的东西,嘿嘿,这就是本文要说的,我们要控制抽奖物品的概率。还有顺便说一句,网上这种小程序几乎没有,很多都是等概率的抽奖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); } } |
(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 5354 | 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 联系到我,大家一起交流学习。
// 计算每个物品在总概率的基础下的概率情况
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;
-------------------------------------------
<!DOCTYPE html> <html lang="en"> <head> <!-- The jQuery library is a prerequisite for all jqSuite products --> <script type="text/ecmascript" src="js/jquery-1.11.0.min.js"></script> <!-- We support more than 40 localizations --> <script type="text/ecmascript" src="js/i18n/grid.locale-en.js"></script> <!-- This is the Javascript file of jqGrid --> <script type="text/ecmascript" src="js/jquery.jqGrid.min.js"></script> <!-- This is the localization file of the grid controlling messages, labels, etc. <!-- A link to a jQuery UI ThemeRoller theme, more than 22 built-in and many more custom --> <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" /> <!-- The link to the CSS that the grid needs --> <link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid-bootstarp.css" /> <meta charset="utf-8" /> <title>jqGrid Loading Data - Million Rows from a REST service</title> </head> <body> <table id="jqGrid"></table> <script type="text/javascript"> $(document).ready(function () { $("#jqGrid").jqGrid({ url: 'http://trirand.com/blog/phpjqgrid/examples/jsonp/getjsonp.php?callback=?&qwery=longorders', mtype: "GET", datatype: "jsonp", colModel: [ { label: 'OrderID', name: 'OrderID', key: true, width: 75 }, { label: 'Customer ID', name: 'CustomerID', width: 150 }, { label: 'Order Date', name: 'OrderDate', width: 150 }, { label: 'Freight', name: 'Freight', width: 150 }, { label:'Ship Name', name: 'ShipName', width: 150 } ], viewrecords: true, width: 480, height: 150, rowNum: 20, pager: "#jqGridPager", gridComplete: function () { var div = $('#jqGrid').closest('.ui-jqgrid-bdiv')[0]////////////// , scrollHeight = div.scrollHeight, scrollTop = 0; var timer= setInterval(function () { scrollTop += 5; div.scrollTop = scrollTop; if (scrollTop >= scrollHeight) scrollTop=0;//设置滚动头为0,重新开始就行了 }, 100); } }); }); </script> </body> </html
相关文章推荐
- JavaScript Date(日期) 对象
- Shell脚本编程的常识
- Go语言2-基础类型及使用
- [转]EF 批量更新/删除数据
- hdu 1170 Balloon Comes
- JavaScript: Names and Versions
- ridge regression
- 黑马程序员-14-java-反射-反射概念及Field、Method、Constructor的应用
- Servlet、servletConfig、ServletContext简介
- VMware三种连接方式bridge, nat, host-only
- VMware三种连接方式bridge, nat, host-only
- 数据结构与算法——二分查找法(Java实现)
- WAMP环境下配置虚拟主机
- Windows 下配置 Opencv 开发环境
- 像写SQL一样编写Java数据应用-TinySqlDsl
- RIP路由协议
- RIP路由协议
- RIP路由协议
- Hibernate HelloWorld-07 双向多对一的映射关系
- SSDB --- queue实现