当前位置:首页 » 操作系统 » java抽奖算法概率

java抽奖算法概率

发布时间: 2023-04-24 03:18:46

A. 用java设计程序。问题描述:公司总人数为100,计划能随机抽出每一个获奖者,一等奖10%,二等奖

调试好了 直接用 public static void main(String[] args) { Random rand = new Random();//定义随机数产生器 List list_One = new ArrayList();//定义一等奖数字集合 List list = null; //用袜蚂举list时 /*list = new ArrayList();//定义100个随机数 集合 for(int i = 0; i < 100;){//产生100个3位数 的随机不重复数字 if(check(rand.nextInt(899) + 100,list)){ i++; } }*/ //用set时物团 Set set = new HashSet(); while(set.size() < 100){ set.add(rand.nextInt(899) + 100); } list = new ArrayList(set); for(int k = 0; k < 5;){//产生5个告碧随机一等奖 if(check(rand.nextInt(100),list,list_One)) k++; } } for(int a = 0; a < list_One.size();a++){ System.out.println(list_One.get(a)); } } /** * 验证该数字是否在集合中 * @param i * @param list * @return */ public static boolean check(int i,List list){ if(list.size() == 0){ list.add(i); }else{ for(int j = 0; j < list.size(); j++){ if(i == list.get(j)){ return false; } } list.add(i); } return true; } /** * 抽取随机数作为中奖的下标(重载) * @param i * @param list * @param list_One * @return */ public static boolean check(int i,List list,List list_One){ if(list.size() == 0){ list_One.add(list.get(i)); }else{ for(int j = 0; j < list.size(); j++){ if(i == list.get(j)){ return false; } } list_One.add(list.get(i)); } return true; }

B. 这个幸运大抽奖的java项目代码,怎么编写呢

public class Test {
public static void main(String[] args) {
int chance = 100;// 中郑渗奖概率,百分之1就是100,千旁郑分之一就是1000
double b = Math.random() * chance;
int i = (int) b;
if (i <运丛颂 1) {
System.out.println("中奖");
}
}
}

C. java程序设计,投骰子,求概率

你这么写吧。抛一次纯枯氏骰子,就是你刚才那个,循环3次,取1-6随败皮机数,取到5,然后youFive++;取不到5,meiFive++。你这样抛10000次,用youFive/10000就很接近真的概率了。 但这是统计的思路,样本可能存在偏差,不知道你做散算这个概率为什么编程?你计算出3个骰子都没有5的概率用1减一下就好了,应该是这个吧1-(5/6)*(5/6)*(5/6)

D. java抽奖程序,可设置不同奖品获得概率

importjava.awt.EventQueue;
importjava.awt.TextArea;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Random;

importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
importjavax.swing.border.EmptyBorder;


{

privateJPanelcontentPane;
privateJTextFieldtextFieldA;
privateJTextFieldtextFieldB;
privateJTextFieldtextFieldC;
privateTextFieldtextField;
;
privateJTextAreatextArea;
/**
*Launchtheapplication.
*/
publicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
publicvoidrun(){
try{
LuckySelectframe=newLuckySelect();
frame.setVisible(true);
}catch(Exceptione){
e.printStackTrace();
}
}
});
}

/**
*Createtheframe.
*/
publicLuckySelect(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,251);
contentPane=newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));
contentPane.setLayout(null);
setContentPane(contentPane);

JLabellblA=newJLabel("A");
lblA.setBounds(10,128,54,15);
contentPane.add(lblA);

JLabellblB=newJLabel("B");
lblB.setBounds(124,128,54,15);
contentPane.add(lblB);

JLabellblC=newJLabel("C");
lblC.setBounds(254,128,54,15);
contentPane.add(lblC);

textFieldA=newJTextField();
textFieldA.setBounds(30,125,66,21);
contentPane.add(textFieldA);
textFieldA.setColumns(10);

textFieldB=newJTextField();
textFieldB.setColumns(10);
textFieldB.setBounds(149,125,66,21);
contentPane.add(textFieldB);

textFieldC=newJTextField();
textFieldC.setColumns(10);
textFieldC.setBounds(264,125,66,21);
contentPane.add(textFieldC);

textField=newTextField();
textField.setBounds(98,167,157,21);
contentPane.add(textField);
textField.setColumns(10);

textFieldResult=newJTextField();
textFieldResult.setBounds(280,167,66,21);
contentPane.add(textFieldResult);
textFieldResult.setColumns(10);
textFieldA.setText("10");
textFieldB.setText("10");
textFieldC.setText("10");

JButtonbutton=newJButton("u62BDu5956");
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
select();
}
});
button.setBounds(0,166,93,23);
contentPane.add(button);

textArea=newJTextArea();
textArea.setBounds(30,31,306,83);
contentPane.add(textArea);
}

protectedvoidselect(){
//TODOAuto-generatedmethodstub
intaNum=Integer.decode(textFieldA.getText());
intbNum=Integer.decode(textFieldB.getText());
intcNum=Integer.decode(textFieldB.getText());
Randomr=newRandom();
intrandom=r.nextInt(aNum+bNum+cNum);
if(random<=aNum){
textFieldA.setText(Integer.toString(Integer.decode(textFieldA.getText())-1));
textArea.append(Integer.toString(random)+"抽中了A ");
}elseif(random<=aNum+bNum){
textFieldB.setText(Integer.toString(Integer.decode(textFieldB.getText())-1));
textArea.append(Integer.toString(random)+"抽中了B ");
}elseif(random<=aNum+bNum+cNum){
textFieldC.setText(Integer.toString(Integer.decode(textFieldC.getText())-1));
textArea.append(Integer.toString(random)+"抽中了C ");
}

}
}

E. java集五福活动概率技术如何实现

  • importjava.util.ArrayList;

  • importjava.util.List;

  • importjava.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】

  • *

  • */

  • publicclassLotteryUtil{

  • /**

  • *定义一个连续集合

  • *集合中元素x满足:(minElement,maxElement]

  • *数学表达式为:minElement<x<=maxElement

  • *

  • */

  • publicclassContinuousList{

  • privatedoubleminElement;

  • privatedoublemaxElement;

  • public者山丛ContinuousList(doubleminElement,doublemaxElement){

  • if(minElement>maxElement){

  • ("区间不合理,minElement不能大于maxElement!");

  • }

  • this.minElement=minElement;

  • this.maxElement=maxElement;

  • }

  • /**

  • *判断当前集合是否包含特定元素

  • *@paramelement

  • *@return

  • */

  • publicbooleanisContainKey(doubleelement){

  • booleanflag=false;

  • if(element>minElement&&element<=maxElement){

  • flag=true;

  • }

  • returnflag;

  • }

  • }

  • privateList<ContinuousList>lotteryList;唯余//概率连续集合

  • privatedoublemaxElement;//这里只需要最大值,最小值默认为0.0

  • /**

  • *构造抽奖集合

  • *@paramlist为奖品的概率

  • */

  • publicLotteryUtil(List<Double>list){

  • lotteryList=newArrayList<ContinuousList>();

  • if(list.size()==0){

  • ("抽奖集合不能为空!");

  • }

  • doubleminElement=0d;

  • ContinuousListcontinuousList=null;

  • for(Doubled:list){

  • minElement=maxElement;

  • maxElement=maxElement+d;

  • continuousList=newContinuousList(minElement,maxElement);

  • lotteryList.add(continuousList);

  • }

  • }

  • /**

  • *进行抽奖操作

  • *返回:奖品的概率list集合中的下标

  • */

  • publicintrandomColunmIndex(){

  • intindex=-1;

  • Randomr=newRandom();

  • doubled=r.nextDouble()*maxElement;//生成首樱0-1间的随机数

  • if(d==0d){

  • d=r.nextDouble()*maxElement;//防止生成0.0

  • }

  • intsize=lotteryList.size();

  • for(inti=0;i<size;i++){

  • ContinuousListcl=lotteryList.get(i);

  • if(cl.isContainKey(d)){

  • index=i;

  • break;

  • }

  • }

  • if(index==-1){

  • ("概率集合设置不合理!");

  • }

  • returnindex;

  • }

  • publicdoublegetMaxElement(){

  • returnmaxElement;

  • }

  • publicList<ContinuousList>getLotteryList(){

  • returnlotteryList;

  • }

  • publicvoidsetLotteryList(List<ContinuousList>lotteryList){

  • this.lotteryList=lotteryList;

  • }

  • }

  • 该工具类的基本思想是,将抽奖概率分布到数轴上,如现有三个抽奖概率10、20、30,将三者依次添加到概率集合中,则构造的数轴为:0~10范围内表示概率10,10~30范围内表示概率为20,30~60范围内表示概率为30,数轴上的长度对应着相应的概率。由这种处理方式可知,概率总和并不需要等于1。该工具类的成功与否在于Random.nextDouble()能否等概率地生成0~1之间的任意一个数。

    对该抽奖工具进行测试,测试类如下:

    [java]view plain

  • packagecom.lottery;

  • importjava.util.ArrayList;

  • importjava.util.HashMap;

  • importjava.util.List;

  • importjava.util.Map;

  • importjava.util.Map.Entry;

  • classResult{

  • privateintindex;

  • privateintsumTime;

  • privateinttime;

  • privatedoubleprobability;

  • privatedoublerealProbability;

  • publicintgetIndex(){

  • returnindex;

  • }

  • publicvoidsetIndex(intindex){

  • this.index=index;

  • }

  • publicintgetTime(){

  • returntime;

  • }

  • publicvoidsetTime(inttime){

  • this.time=time;

  • }

  • publicintgetSumTime(){

  • returnsumTime;

  • }

  • publicvoidsetSumTime(intsumTime){

  • this.sumTime=sumTime;

  • }

  • publicdoublegetProbability(){

  • returnprobability;

  • }

  • (){

  • returnrealProbability;

  • }

  • publicvoidsetRealProbability(doublerealProbability){

  • this.realProbability=realProbability;

  • }

  • publicResult(){

  • }

  • publicResult(intindex,intsumTime,inttime,doublerealProbability){

  • this.setIndex(index);

  • this.setTime(time);

  • this.setSumTime(sumTime);

  • this.setRealProbability(realProbability);

  • }

  • publicStringtoString(){

  • return"索引值:"+index+",抽奖总数:"+sumTime+",抽中次数:"+time+",概率:"

  • +realProbability+",实际概率:"+(double)time/sumTime;

  • }

  • }

  • publicclassTestLottery{

  • staticfinalintTIME=100000;

  • publicstaticvoiditeratorMap(Map<Integer,Integer>map,List<Double>list){

  • for(Entry<Integer,Integer>entry:map.entrySet()){

  • intindex=entry.getKey();

  • inttime=entry.getValue();

  • Resultresult=newResult(index,TIME,time,list.get(index));

  • System.out.println(result);

  • }

  • }

  • publicstaticvoidmain(String[]args){

  • //构造概率集合

  • List<Double>list=newArrayList<Double>();

  • list.add(20d);

  • list.add(80d);

  • list.add(50d);

  • list.add(30d);

  • LotteryUtilll=newLotteryUtil(list);

  • doublesumProbability=ll.getMaxElement();

  • Map<Integer,Integer>map=newHashMap<Integer,Integer>();

  • for(inti=0;i<TIME;i++){

  • intindex=ll.randomColunmIndex();

  • if(map.containsKey(index)){

  • map.put(index,map.get(index)+1);

  • }else{

  • map.put(index,1);

  • }

  • }

  • for(inti=0;i<list.size();i++){

  • doubleprobability=list.get(i)/sumProbability;

  • list.set(i,probability);

  • }

  • iteratorMap(map,list);

  • }

  • }


  • 运行结果:


    由结果可知,抽奖100000时, 得到的实际概率基本与正式概率相当。

    以下说明此类调用方式:

    [java]view plain

  • publicLotteryUtil(List<Double>list)

  • 说明:构造方法,传入参数为一个概率集合

  • [java]view plain

  • publicintrandomColunmIndex()

  • 功能:进行抽奖操作,返回List集合的索引下标,此下标对应的概率的奖品即为抽中的奖品




F. 求一段JAVA的概率算法

public class Zhuq {
public static void main(String[] args) {
List<Person> listP=new ArrayList<Person>();
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 Comparator<Person>() {

@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 + "]";
}

}
//本质还是随机数

G. 一等奖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,表示没有中灶弊巧奖。

H. 抽奖概率的计算

/*
* 下面方法是在考虑奖项有名额限制的情运让况下
* 暂定一等奖1名 二等奖 2名 3等级3名 幸运奖10名
* 中奖方法适用math.random(1000)
*/
Integer one = 1;
Integer two = 2;
Integer three = 3;
Integer lucky = 10;
public Integer Lottery(){//返回值:1-一等奖 2-二等奖 3-三等奖 4-幸运奖 0-不中奖
Integer ranNum = (int)(Math.random()*1000);
System.out.println(ranNum);
if(ranNum>=0&&ranNum<10)//获奖范围内凯悄滑
{
if(ranNum==0)//获得盯腊0 概率为0.1%
{
if(one>0)
{
one--;
return 1;
}
if(two>0)
{
two--;
return 2;
}
if(three>0)
{
three--;
return 3;
}
if(lucky>0)
{
lucky--;
return 4;
}
}

if(ranNum>=0&&ranNum<2)
{
if(two>0)
{
two--;
return 2;
}
if(three>0)
{
three--;
return 3;
}
if(lucky>0)
{
lucky--;
return 4;
}
}

if(ranNum>=0&&ranNum<5)
{

if(three>0)
{
three--;
return 3;
}
if(lucky>0)
{
lucky--;
return 4;
}
}

if(lucky>0)
{
lucky--;
return 4;
}
}

return 0;
}

I. JAVA抽奖的算法

那你可以再1-10000之间随即嘛,概率小的奖品设置为1-10,概率大的设置为11-1000,最后随即出来的数判断一下在那个区间就可以了.
实体类,就是写一个JAVA BEAN嘛,里面记载奖品名字和数量啊,不懂可以追问

J. 求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(r<d){//如果随机数小于概率 那么中奖
n--;//奖品数量-1
isLottery = true;
}
m--;//奖券数量-1
return isLottery;
}

}

热点内容
苹果好用的解压软件 发布:2025-05-17 22:42:23 浏览:381
我的世界服务器莫名崩溃 发布:2025-05-17 22:40:57 浏览:477
我的世界utc服务器ip 发布:2025-05-17 22:36:19 浏览:740
新闻压缩要素 发布:2025-05-17 22:22:11 浏览:118
耳机没有声音怎么办安卓 发布:2025-05-17 22:16:29 浏览:583
bc8android导航 发布:2025-05-17 22:15:50 浏览:639
什么配置的车标好 发布:2025-05-17 21:41:20 浏览:203
linux支持线程 发布:2025-05-17 21:26:14 浏览:184
元神队伍配置都由什么组成 发布:2025-05-17 21:20:18 浏览:477
闲鱼和安卓哪个赚钱 发布:2025-05-17 21:15:56 浏览:584