「java的概率算法」java概率随机数

博主:adminadmin 2022-12-09 16:36:08 60

本篇文章给大家谈谈java的概率算法,以及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的概率算法

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中如何实现概率算法,比如打架小游戏,普通攻击概率为80%,暴击为20%。 谢谢啦

几率的算法,大家不信可以进游戏看看自己各种技能伤害加成是不是这样,例如鸟的大招... 觉醒+自身一共20%暴,2+4暴击套30%,这就是50%了

关于java的概率算法和java概率随机数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-09,除非注明,否则均为首码项目网原创文章,转载请注明出处。