中奖率 1%,每人抽 10 次,也就是说 10 个人就可以抽 100 次,概率上来说 10 个人必中奖?假设逻辑是这样的,可以这样设计:
专注于为中小企业提供网站设计制作、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业农安免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
1、先写定义一个表示概率的数组 ratio,这个数组最多可以存 100 个数字,每个数字的值不能重复,范围是 0 到 100,表示 100%。现在你的中奖率是 1%,那么就存一个数字。
2、定义一个是否中将的布尔型变量 win;
每次抽奖用随机正整数对 100 取余,再将结果拿到 ratio 数组中查找,如果找到就将 win 赋值为 true,表示中奖。如果没找到就对 win 赋值为 false,表示没有中奖。
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class LuckySelect extends JFrame {
private JPanel contentPane;
private JTextField textFieldA;
private JTextField textFieldB;
private JTextField textFieldC;
private TextField textField;
private JTextField textFieldResult;
private JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LuckySelect frame = new LuckySelect();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LuckySelect() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 251);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JLabel lblA = new JLabel("A");
lblA.setBounds(10, 128, 54, 15);
contentPane.add(lblA);
JLabel lblB = new JLabel("B");
lblB.setBounds(124, 128, 54, 15);
contentPane.add(lblB);
JLabel lblC = new JLabel("C");
lblC.setBounds(254, 128, 54, 15);
contentPane.add(lblC);
textFieldA = new JTextField();
textFieldA.setBounds(30, 125, 66, 21);
contentPane.add(textFieldA);
textFieldA.setColumns(10);
textFieldB = new JTextField();
textFieldB.setColumns(10);
textFieldB.setBounds(149, 125, 66, 21);
contentPane.add(textFieldB);
textFieldC = new JTextField();
textFieldC.setColumns(10);
textFieldC.setBounds(264, 125, 66, 21);
contentPane.add(textFieldC);
textField = new TextField();
textField.setBounds(98, 167, 157, 21);
contentPane.add(textField);
textField.setColumns(10);
textFieldResult = new JTextField();
textFieldResult.setBounds(280, 167, 66, 21);
contentPane.add(textFieldResult);
textFieldResult.setColumns(10);
textFieldA.setText("10");
textFieldB.setText("10");
textFieldC.setText("10");
JButton button = new JButton("\u62BD\u5956");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
select();
}
});
button.setBounds(0, 166, 93, 23);
contentPane.add(button);
textArea = new JTextArea();
textArea.setBounds(30, 31, 306, 83);
contentPane.add(textArea);
}
protected void select() {
// TODO Auto-generated method stub
int aNum = Integer.decode(textFieldA.getText());
int bNum = Integer.decode(textFieldB.getText());
int cNum = Integer.decode(textFieldB.getText());
Random r = new Random();
int random = r.nextInt(aNum + bNum + cNum);
if(random = aNum){
textFieldA.setText(Integer.toString(Integer.decode(textFieldA.getText()) - 1));
textArea.append(Integer.toString(random) + "抽中了A\n");
}else if(random = aNum + bNum){
textFieldB.setText(Integer.toString(Integer.decode(textFieldB.getText()) - 1));
textArea.append(Integer.toString(random) + "抽中了B\n");
}else if(random = aNum + bNum + cNum){
textFieldC.setText(Integer.toString(Integer.decode(textFieldC.getText()) - 1));
textArea.append(Integer.toString(random) + "抽中了C\n");
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 抽奖工具类,概率和可以不等于1
* 概率为百分数去掉百分号的部分,如10%,则为10
* 抽奖操作如下:
* 1.输入抽奖概率集合,【抽奖概率集合为{10.0, 20.0, 30.0}】
* 2.生成连续集合, 【生成的连续集合为{(0.0, 10.0],(10.0, 30.0],(30.0, 60.0]}】
* 3.生成随机数, 【生成方法为 random.nextDouble() * maxElement】
* 4.判断随机数在哪个区间内,返回该区间的index【生成了随机数12.001,则它属于(10.0, 30.0],返回 index = 1】
*
*/
public class LotteryUtil {
/**
* 定义一个连续集合
* 集合中元素x满足:(minElement,maxElement]
* 数学表达式为:minElement x = maxElement
*
*/
public class ContinuousList {
private double minElement;
private double maxElement;
public ContinuousList(double minElement, double maxElement){
if(minElement maxElement){
throw new IllegalArgumentException("区间不合理,minElement不能大于maxElement!");
}
this.minElement = minElement;
this.maxElement = maxElement;
}
/**
* 判断当前集合是否包含特定元素
* @param element
* @return
*/
public boolean isContainKey(double element){
boolean flag = false;
if(element minElement element = maxElement){
flag = true;
}
return flag;
}
}
private ListContinuousList lotteryList; //概率连续集合
private double maxElement; //这里只需要最大值,最小值默认为0.0
/**
* 构造抽奖集合
* @param list 为奖品的概率
*/
public LotteryUtil(ListDouble list){
lotteryList = new ArrayListContinuousList();
if(list.size() == 0){
throw new IllegalArgumentException("抽奖集合不能为空!");
}
double minElement = 0d;
ContinuousList continuousList = null;
for(Double d : list){
minElement = maxElement;
maxElement = maxElement + d;
continuousList = new ContinuousList(minElement, maxElement);
lotteryList.add(continuousList);
}
}
/**
* 进行抽奖操作
* 返回:奖品的概率list集合中的下标
*/
public int randomColunmIndex(){
int index = -1;
Random r = new Random();
double d = r.nextDouble() * maxElement; //生成0-1间的随机数
if(d == 0d){
d = r.nextDouble() * maxElement; //防止生成0.0
}
int size = lotteryList.size();
for(int i = 0; i size; i++){
ContinuousList cl = lotteryList.get(i);
if(cl.isContainKey(d)){
index = i;
break;
}
}
if(index == -1){
throw new IllegalArgumentException("概率集合设置不合理!");
}
return index;
}
public double getMaxElement() {
return maxElement;
}
public ListContinuousList getLotteryList() {
return lotteryList;
}
public void setLotteryList(ListContinuousList lotteryList) {
this.lotteryList = lotteryList;
}
}
该工具类的基本思想是,将抽奖概率分布到数轴上,如现有三个抽奖概率10、20、30,将三者依次添加到概率集合中,则构造的数轴为:0~10范围内表示概率10,10~30范围内表示概率为20,30~60范围内表示概率为30,数轴上的长度对应着相应的概率。由这种处理方式可知,概率总和并不需要等于1。该工具类的成功与否在于Random.nextDouble()能否等概率地生成0~1之间的任意一个数。
对该抽奖工具进行测试,测试类如下:
[java] view plain copy
package com.lottery;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
class Result{
private int index;
private int sumTime;
private int time;
private double probability;
private double realProbability;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getSumTime() {
return sumTime;
}
public void setSumTime(int sumTime) {
this.sumTime = sumTime;
}
public double getProbability() {
return probability;
}
public double getRealProbability() {
return realProbability;
}
public void setRealProbability(double realProbability) {
this.realProbability = realProbability;
}
public Result(){
}
public Result(int index, int sumTime, int time, double realProbability) {
this.setIndex(index);
this.setTime(time);
this.setSumTime(sumTime);
this.setRealProbability(realProbability);
}
public String toString(){
return "索引值:" + index + ",抽奖总数:" + sumTime + ",抽中次数:" + time + ",概率:"
+ realProbability + ",实际概率:" + (double)time/sumTime;
}
}
public class TestLottery {
static final int TIME = 100000;
public static void iteratorMap(MapInteger, Integer map, ListDouble list){
for(EntryInteger, Integer entry : map.entrySet()){
int index = entry.getKey();
int time = entry.getValue();
Result result = new Result(index, TIME, time, list.get(index));
System.out.println(result);
}
}
public static void main(String[] args) {
//构造概率集合
ListDouble list = new ArrayListDouble();
list.add(20d);
list.add(80d);
list.add(50d);
list.add(30d);
LotteryUtil ll = new LotteryUtil(list);
double sumProbability = ll.getMaxElement();
MapInteger, Integer map = new HashMapInteger, Integer();
for(int i = 0; i TIME; i++){
int index = ll.randomColunmIndex();
if(map.containsKey(index)){
map.put(index, map.get(index) + 1);
}else{
map.put(index, 1);
}
}
for(int i = 0; i list.size(); i++){
double probability = list.get(i) / sumProbability;
list.set(i, probability);
}
iteratorMap(map, list);
}
}
运行结果:
由结果可知,抽奖100000时, 得到的实际概率基本与正式概率相当。
以下说明此类调用方式:
[java] view plain copy
public LotteryUtil(ListDouble list)
说明:构造方法,传入参数为一个概率集合
[java] view plain copy
public int randomColunmIndex()
功能:进行抽奖操作,返回List集合的索引下标,此下标对应的概率的奖品即为抽中的奖品
优先级规则使高等奖尽量在后期抽出
import java.util.LinkedList;
import java.util.List;
public class GetGift {
// 奖品仓库
private ListGift gifts = new LinkedListGift();
public GetGift() {
// 生成一堆奖品放进奖品仓库
// 一等奖一个优先级1,二等奖两个优先级2。。。20等奖20个优先级20
for (int i = 1; i = 20; i++) {
GiftType gt = new GiftType(i + "等奖", i, i);
for (int j = 1; j = i; j++) {
gifts.add(new Gift(i + "等奖——第" + j + "号", gt));
}
}
}
// 抽奖
public synchronized Gift getGift() {
int randomNumber = (int) (Math.random() * total());
int priority = 0;
for (Gift g : gifts) {
priority += g.getType().getPriority();
if (priority = randomNumber) {
// 从奖品库移出奖品
gifts.remove(g);
return g;
}
}
// 抽奖次数多于奖品时,没有奖品
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
GetGift gg = new GetGift();
// 一共生成210个奖品,抽210次,多抽显示null
for (int i = 0; i 210; i++) {
System.out.println(gg.getGift());
}
}
// 计算总优先级,内部使用
private int total() {
int result = 0;
for (Gift g : gifts) {
result += g.getType().getPriority();
}
return result;
}
}
// 记录奖品的信息
// 如1等奖共1个,优先级为1最难抽
class GiftType {
// 名字(如1等奖)
private String name;
// 这种奖品的数量,数量越大越容易抽到
private int quantity;
// 这种奖品的优先级,最小为1,数越大越容易抽到
private int priority;
public GiftType(String name, int quantity, int priority) {
this.name = name;
this.quantity = quantity;
this.priority = priority;
}
public int getPriority() {
return priority;
}
@Override
public String toString() {
return "GiftType [name=" + name + ", quantity=" + quantity + ", priority=" + priority + "]";
}
}
// 奖品
class Gift {
// 每个奖品有唯一id,抽奖不会重复,格式为"16等奖——第8号"
private String id;
// 这个奖品的类别
private GiftType type;
public Gift(String id, GiftType type) {
this.id = id;
this.type = type;
}
public GiftType getType() {
return type;
}
@Override
public String toString() {
return "Gift [id=" + id + ", type=" + type + "]";
}
}
抽取问题, 重点是 同一个学号不能重复被抽取.
解决办法很多,
比如数组可以使用下标来标记,号码是否被使用,使用了就继续下一次抽取
也可以使用集合来抽取,把集合顺序打乱,然后随便抽几个就可以了
参考代码:数组法
import java.util.Random;
public class Test {
public static void main(String[] args) {
int stuNums=30;
int[] nums=new int[stuNums];//存储学号的数组
boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了
for (int i = 0; i stuNums; i++) {
nums[i]=i+1;//给学号赋值
}
Random r=new Random();
while(true){
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true; //标记已经被使用过了
break;
}
}
for (int i = 0; i 2; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已经被抽取过了 ,那么i建议,再次循环
}
}
for (int i = 0; i 3; i++) {
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}
集合法
import java.util.ArrayList;
import java.util.Collections;
public class Test2 {
public static void main(String[] args) {
int stuNums=20;
ArrayListInteger list=new ArrayListInteger();
for (int i = 0; i stuNums; i++) {
list.add(i+1);
}
System.out.println("有序"+list);
Collections.shuffle(list);//打乱顺序
System.out.println("乱序"+list);
System.out.println("A等"+list.get(0));
System.out.println("B等"+list.get(1));
System.out.println("B等"+list.get(2));
System.out.println("C等"+list.get(3));
System.out.println("C等"+list.get(4));
System.out.println("C等"+list.get(5));
}
}
public class Lottery {
private int m = 1000;//发放奖券的数量
private int n = 2;//奖品的数量
public boolean getLottery(){
boolean isLottery = false;
double d = (double)n/(double)m;//中奖概率
double r = Math.random();//0~1之间的随机数,包括0
if(rd){//如果随机数小于概率 那么中奖
n--;//奖品数量-1
isLottery = true;
}
m--;//奖券数量-1
return isLottery;
}
}