比如:
<?php
$prize_arr = array(
'0' => array('id'=>1,'prize'=>'平板电脑','v'=>3),
'1' => array('id'=>2,'prize'=>'数码相机','v'=>5),
'2' => array('id'=>3,'prize'=>'音箱设备','v'=>10),
'3' => array('id'=>4,'prize'=>'4G优盘','v'=>12),
'4' => array('id'=>5,'prize'=>'Q币10元','v'=>20),
'5' => array('id'=>6,'prize'=>'下次没准就能中哦','v'=>50),
);
foreach ($prize_arr as $key => $val) {
$arr[$val['id']] = $val['v'];
}
$rid = getRand($arr); /*根据概率获取奖项id*/
$res['msg'] = ($rid==6)?0:1;
$res['prize'] = $prize_arr[$rid-1]['prize']; /*中奖项*/
echo json_encode($res);exit;
/*计算概率*/
function getRand($proArr) {
$result = '';
/*概率数组的总概率精度*/
$proSum = array_sum($proArr);
/*概率数组循环*/
foreach ($proArr as $key => $proCur) {
/*相当于每次只能够抽取其中的一个奖项,没抽中就开始抽下一个奖项,直到最后*/
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
/*表示抽中奖了,该奖品应该要减一*/
$result = $key;
break;
} else {
/*没有抽中就不能再抽了,直到敲到下一个金蛋*/
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
}
?>
代码如下:
public class Guess {
private int id;
private String prize;
private int v;
public Guess(int id, String prize, int v) {
super();
this.id = id;
this.prize = prize;
this.v = v;
}
public i1nt getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
public int getV() {
return v;
}
public void setV(int v) {
this.v = v;
}
}
public class WinPrize {
private int mes;
private String prize;
public int getMes() {
return mes;
}
public WinPrize(int mes, String prize) {
super();
this.mes = mes;
this.prize = prize;
}
public WinPrize() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "mes:" + mes + ", prize:" + prize;
}
public void setMes(int mes) {
this.mes = mes;
}
public String getPrize() {
return prize;
}
public void setPrize(String prize) {
this.prize = prize;
}
}
public class TestPHP {
public void guess_prize(){
Map prize_attr=new HashMap();
Integer[] arr=null;
prize_attr.put(0, new Guess(1,"平板电脑",3));
prize_attr.put(1, new Guess(2,"数码相机",5));
prize_attr.put(2, new Guess(3,"音箱设备",10));
prize_attr.put(3, new Guess(4,"4G优盘",12));
prize_attr.put(4, new Guess(5,"Q币10元",20));
prize_attr.put(5, new Guess(6,"下次没准就能中哦",50));
arr=new Integer[prize_attr.size()+1];
for(Guess g:prize_attr.values()){
//System.out.println(prize_attr.size());
arr[g.getId()]=g.getV();
}
WinPrize res=new WinPrize();
int rid=getRand(arr);
if(prize_attr.get(rid)==null){
res.setMes(0);
res.setPrize("下次没准就能中哦");
}else{
res.setMes(rid==6?0:1);
res.setPrize(prize_attr.get(rid).getPrize());
}
System.out.println(res);
}
public int getRand(Integer[] arr){
int sum=0;
int result=0;
//System.out.println(arr);
for(int i=1;i<arr.length;i++){
sum+=arr[i];
}
for(int i=1;i<arr.length;i++){
Random r=new Random();
int random=r.nextInt(sum)+1;//arr长度为6,生成1-6的随机数
if(random<=arr[i]){
result=i;
break;
}else{
sum-=arr[i];
}
}
return result;
}
public static void main(String[] args) {
new TestPHP().guess_prize();
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。