0
点赞
收藏
分享

微信扫一扫

【前端】写个大转盘抽奖概率程序


目录

​​一、需求描述​​

​​二、程序实现​​

​​三、界面​​

​​四、运行结果​​

​​五、JAVA实现​​

一、需求描述

1. 大转盘有5个格子,每个格子放置对应的奖品和可以设置概率

2. 抽奖次数有10次,20次,30次,每次抽中转盘中一个奖品

二、程序实现

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>抽奖脚本</title>
</head>
<body>
<table>
<tr>
<th>奖品ID</th>
<th>奖品价值(金币)</th>
<th>奖品概率(整数0-10000)</th>
</tr>
<tr>
<th><input type="text" name="prize[0][id]" class="prize" id="prize1_id" value="1"></th>
<th><input type="text" name="prize[0][cost]" class="prize" id="prize1_cost" value="40"></th>
<th><input type="text" name="prize[0][ratio]" class="prize" id="prize1_ratio" value="100"></th>
</tr>
<tr>
<th><input type="text" name="prize[1][id]" class="prize" id="prize2_id" value="2"></th>
<th><input type="text" name="prize[1][cost]" class="prize" id="prize2_cost" value="30"></th>
<th><input type="text" name="prize[1][ratio]" class="prize" id="prize2_ratio" value="900"></th>
</tr>
<tr>
<th><input type="text" name="prize[2][id]" class="prize" id="prize3_id" value="3"></th>
<th><input type="text" name="prize[2][cost]" class="prize" id="prize3_cost" value="20"></th>
<th><input type="text" name="prize[2][ratio]" class="prize" id="prize3_ratio" value="2000"></th>
</tr>
<tr>
<th><input type="text" name="prize[3][id]" class="prize" id="prize4_id" value="4"></th>
<th><input type="text" name="prize[3][cost]" class="prize" id="prize4_cost" value="9"></th>
<th><input type="text" name="prize[3][ratio]" class="prize" id="prize4_ratio" value="3000"></th>
</tr>
<tr>
<th><input type="text" name="prize[4][id]" class="prize" id="prize5_id" value="5"></th>
<th><input type="text" name="prize[4][cost]" class="prize" id="prize5_cost" value="1"></th>
<th><input type="text" name="prize[4][ratio]" class="prize" id="prize5_ratio" value="4000"></th>
</tr>
</table>

<div>
抽奖单价:<input type="text" name="price" id="price" value="10">
抽奖次数:<input type="text" name="num" id="num" value="10">
</div>

<div id="result">
抽奖花费金币:<span id="cost"></span><br>
抽奖总价值:<span id="total_wealth"></span><br>
<button id="sub">执行</button>
<br>
抽奖结果:
<table id="result_item">
<!--<tr>-->
<!--<td>奖品ID:<span class="p-id"></span></td>-->
<!--<td>奖品数量:<span class="p-num"></span></td>-->
<!--<td>奖品总值:<span class="p-cost"></span></td>-->
<!--</tr>-->
</table>
</div>
</body>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>

$('#sub').on('click', function () {
//奖品1
var prize1 = {
id: $('#prize1_id').val(),
cost: $('#prize1_cost').val(),
ratio: $('#prize1_ratio').val()
};
var prize2 = {
id: $('#prize2_id').val(),
cost: $('#prize2_cost').val(),
ratio: $('#prize2_ratio').val()
};
var prize3 = {
id: $('#prize3_id').val(),
cost: $('#prize3_cost').val(),
ratio: $('#prize3_ratio').val()
};
var prize4 = {
id: $('#prize4_id').val(),
cost: $('#prize4_cost').val(),
ratio: $('#prize4_ratio').val()
};
var prize5 = {
id: $('#prize5_id').val(),
cost: $('#prize5_cost').val(),
ratio: $('#prize5_ratio').val()
};
var prizeRule = [prize1, prize2, prize3, prize4, prize5];
console.log(prizeRule);

var price = $('#price').val();
var num = $('#num').val();
console.log(price);
console.log(num);

//生成抽奖数组
var prizes = [];

for (p in prizeRule) {
console.log(prizeRule[p]);
for (var i = 0; i < prizeRule[p].ratio; i++) {
prizes.push(prizeRule[p])
}
}
;

//记录用户中的奖品
var userPrize = [];

for (var i = 0; i < num; i++) {
var index = randomNum(0, 9999);
userPrize.push(prizes[index]);
}

console.log(userPrize);

var prizeTotal = [];

for (p in userPrize) {
console.log(userPrize[p]);
if (prizeTotal[userPrize[p].id] !== undefined) {
var ob = prizeTotal[userPrize[p].id];
ob.num = ob.num + 1;
ob.cost = parseInt(ob.cost) + parseInt(userPrize[p].cost);
prizeTotal[userPrize[p].id] = ob;
} else {
var obtain = {
id: userPrize[p].id,
num: 1,
cost: userPrize[p].cost
};
prizeTotal[userPrize[p].id] = obtain;
}
}
;

console.log(prizeTotal);

var str = '';
for (var i = 0; i < prizeTotal.length; i++) {
if (prizeTotal[i] !== undefined) {
str += ' <tr>\n' +
' <td>奖品ID:<span class="p-id">' + prizeTotal[i].id + '</span></td>\n' +
' <td>奖品数量:<span class="p-num">' + prizeTotal[i].num + '</span></td>\n' +
' <td>奖品总值:<span class="p-cost">' + prizeTotal[i].cost + '</span></td>' +
' </tr>';
}
}
$('#result_item').append(str);

//计算奖品总价值
var total_wealth = 0;
var str = '';
for (var i = 0; i < userPrize.length; i++) {
total_wealth += parseInt(userPrize[i].cost);
str += '';
}
$('#cost').html(parseInt(price) * parseInt(num));
$('#total_wealth').html(total_wealth);

});

/**
* 生成从minNum到maxNum的随机数
* @param minNum 开始
* @param maxNum 结束
*/
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
}

</script>

三、界面

【前端】写个大转盘抽奖概率程序_数组

四、运行结果

【前端】写个大转盘抽奖概率程序_javascript_02

五、JAVA实现

package lottery;

import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @author Marion
* @date 2021/6/30 09:55
*/
public class Lottery {

public static void main(String[] args) {
/**
* 大转盘抽奖
* 1. 初级转盘格子5格,高级8格,奖券6格
* 2. 转盘每格可以设置概率与奖品(与金币等价)
* 3. 总奖品价值
* 1. 奖池公式:prizeTotal(总价值)= + prize(单个奖品价值)* ratio(单个奖品概率)
* 2. 每次抽奖10金币
* 4. 判断如何设置奖品和概率合理
*/

/**
* 1. 定义转盘格子
* 2. 设置转盘格子初始价值与概率,中奖概率最小0.01%
* 3. 随机整数范围 = 0 ~ 10000
* 4. 计算10000次抽奖后,花费金币,产出金币,以及差值来调整中奖概率
*/
int grid = 5;
Prize prize1 = new Prize(1, 100, 40);
Prize prize2 = new Prize(2, 900, 30);
Prize prize3 = new Prize(3, 2000, 20);
Prize prize4 = new Prize(4, 3000, 9);
Prize prize5 = new Prize(5, 4000, 1);

//生成数组
Set<Prize> prizeRule = Set.of(prize1, prize2, prize3, prize4, prize5);

//随机数组
Prize[] prizes = new Prize[10000];

AtomicInteger j = new AtomicInteger(0);
prizeRule.forEach(prize -> {
for (int i = 0; i < prize.getRatio(); i++) {
prizes[j.get()] = prize;
j.getAndIncrement();
}
System.out.println("奖品" + prize.getId() +
", 奖品概率=" + new DecimalFormat("0.00").format((double) prize.getRatio() / 10000) +
", 奖品价值=" + prize.getCost());
});

// System.out.println("数组大小=" + prizes.length);
//每次生成随机数0~9999
//抽奖10000次
int lottery = 1000;
List<Prize> userPrize = new ArrayList<>();
for (int i = 0; i < lottery; i++) {
int index = new Random().nextInt(9999);
// System.out.println(index);
userPrize.add(prizes[index]);
// System.out.println(prizes[index].toString());
}

// System.out.println("中奖大小=" + userPrize.size());
int value = 0;
//抽奖
Map<Integer, Integer> num = new HashMap<>();
for (Prize prize : userPrize) {
// System.out.println(prize);
value = value + prize.getCost();

Integer no = num.get(prize.getId());
if (no != null) {
num.put(prize.getId(), no + 1);
} else {
num.put(prize.getId(), 1);
}
}

System.out.println("消耗金币=" + 10 * lottery + ", 中奖价值=" + value);

for (Map.Entry<Integer, Integer> entry : num.entrySet()) {
System.out.println("抽奖获得奖品id=" + entry.getKey() + ", num=" + entry.getValue());
}
}

public static class Prize {

/**
* 奖品ID
*/
private int id;

/**
* 奖品概率,最小0.01%
*/
private int ratio;

/**
* 奖品价值金币
*/
private int cost;

public Prize(int id, int ratio, int cost) {
this.id = id;
this.ratio = ratio;
this.cost = cost;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getRatio() {
return ratio;
}

public void setRatio(int ratio) {
this.ratio = ratio;
}

public int getCost() {
return cost;
}

public void setCost(int cost) {
this.cost = cost;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Prize prize = (Prize) o;
return id == prize.id &&
ratio == prize.ratio &&
cost == prize.cost;
}

@Override
public int hashCode() {
return Objects.hash(id, ratio, cost);
}

@Override
public String toString() {
return "Prize{" +
"id=" + id +
", ratio=" + ratio +
", cost=" + cost +
'}';
}
}

}

举报

相关推荐

0 条评论