「抽奖概率算法java」抽奖概率算法转盘
本篇文章给大家谈谈抽奖概率算法java,以及抽奖概率算法转盘对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、求java算法:根据物品的数量来确定抽奖的概率(当物品数量为0时无论如何都不可能选到)
- 2、java抽奖程序,可设置不同奖品获得概率
- 3、一等奖1个2等奖5个1等奖概率1%每人抽10次java怎么写
- 4、JAVA抽奖的算法
- 5、求一段JAVA的概率算法
求java算法:根据物品的数量来确定抽奖的概率(当物品数量为0时无论如何都不可能选到)
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;
}
}
java抽奖程序,可设置不同奖品获得概率
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");
}
}
}
一等奖1个2等奖5个1等奖概率1%每人抽10次java怎么写
中奖率 1%,每人抽 10 次,也就是说 10 个人就可以抽 100 次,概率上来说 10 个人必中奖?假设逻辑是这样的,可以这样设计:
1、先写定义一个表示概率的数组 ratio,这个数组最多可以存 100 个数字,每个数字的值不能重复,范围是 0 到 100,表示 100%。现在你的中奖率是 1%,那么就存一个数字。
2、定义一个是否中将的布尔型变量 win;
每次抽奖用随机正整数对 100 取余,再将结果拿到 ratio 数组中查找,如果找到就将 win 赋值为 true,表示中奖。如果没找到就对 win 赋值为 false,表示没有中奖。
JAVA抽奖的算法
优先级规则使高等奖尽量在后期抽出
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 + "]";
}
}
求一段JAVA的概率算法
public class Zhuq {
public static void main(String[] args) {
ListPerson listP=new ArrayListPerson();
listP.add(new Person("小李", "1", 200));
listP.add(new Person("小王", "2", 210));
listP.add(new Person("小赵", "3", 230));
listP.add(new Person("小孙", "4", 100));
listP.add(new Person("小钱", "5", 3));
listP.sort(new ComparatorPerson() {
@Override
public int compare(Person o1, Person o2) {
// TODO Auto-generated method stub
return (((Person)o1).count)*(Math.random()*10+1)(((Person)o2).count)*(Math.random()*10+1)?-1:1;
}
});
System.out.println(listP);
}
}
class Person {
String personName;
String id;
int count;
public Person(String personName, String id, int count) {
super();
this.personName = personName;
this.id = id;
this.count = count;
}
@Override
public String toString() {
return "Person [personName=" + personName + ", id=" + id + ", count=" + count + "]";
}
}
//本质还是随机数
关于抽奖概率算法java和抽奖概率算法转盘的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。