
求java算法:根据物品的数量来确定抽奖的概率(当物品数量为0时无论如何都不可能选到) 20
1个回答
展开全部
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;
}
}
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;
}
}
更多追问追答
追问
是像这样的:
有1个数组,值分别为a1=10,a2=20,a3=30,a4=40,现在需要产生一个随机数,该随机数的最大值为该数组的值的和:10+20+30+40=100,区间为Random(1,100),
请教如何判断该随机数是落在哪个区间的呢?
区间:a1(1-10),a2(11-30),a3(31-60),a4(61-100).,区间的大小可以改变
追答
产生的随机数x如果是1<=x<=10就是a1 11<=x<=30就是a2 ......以此类推就好了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询