導航:首頁 > 編程語言 > java概率計算

java概率計算

發布時間:2023-02-04 18:33:04

java中如何讓A的輸出的概率為50%B輸出的概率為30%C輸出的概率為20%

publicclassTest{

publicstaticvoidmain(String[]args){
doubled=Math.random();//生成一個0~1的隨機數
if(d<=0.5){
System.out.println("A");//50%概率
}elseif(d<=0.8){
System.out.println("B");//30%概率
}else{
System.out.println("C");//20%概率
}
}
}

② JAVA中如何實現概率演算法,比如打架小游戲,普通攻擊概率為80%,暴擊為20%。 謝謝啦

幾率的演算法,大家不信可以進游戲看看自己各種技能傷害加成是不是這樣,例如鳥的大招... 覺醒+自身一共20%暴,2+4暴擊套30%,這就是50%了

③ 求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;
}

}

④ java投擲骰子,求點數的出現概率

很簡單 。
int [] num = new int[3]; //數組長度為3
int diag=0; //統計出現5的次數
for(int i=0;i>3;i++){ //擲骰子三次 ,也就是三個骰子
隨機數字 num[i]=(int)(Math.random()*5)+1; //隨機1-6的數字
if(num[i]==5){
diag++; //當骰子等於5的時候,統計次數加1;
}
}
double a=diag/3; //骰子出現5的次數,除以骰子總數,得到概率。
System.out.println(a);

⑤ 求一段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 + "]";
}

}
//本質還是隨機數

⑥ java隨機生成1-100之間的數10000次,計算出88出現得概率

package zhangyw.test;

import java.util.Random;

public class T {

public static void main(String[] args) throws Exception {
int count = 0;
Random random = new Random();
for (int i = 0; i < 10000; i++) {
int value = random.nextInt(100) + 1; //nextInt方法返回的值在0-99之間,所以需要加1
if (value == 88) {
count++;
}
}

System.out.println(count / 100.0 + "%");
}

}

⑦ java程序中概率問題

用概率模型,先隨機一次看取用哪個概率,隨後再隨機一次。代碼示例如下: import java.util.Random;public class HelloWorld { public static void main(String[] args) { Random random = new Random(); double p1=0.7; //1~4的概率 double p=(...

⑧ JAVA里 用取余算概率 應該怎麼算

可以通過多次結果來統計出成功率int i=0,j=0;
while(j++<100000){
if(Math.abs(ap.nextInt())% 51 <= i1)i++;
}
//成功率應該就是 i/100000.0了 ,要更精確 把循環次數增加就好~

⑨ java中概率的問題

你的問題描述不清。
如果是別的數字是均等的,那把一個單獨處理,別的數字分享17/20的概率。實際上是一個映射的問題。具體實現就是拿20個數字做random,然後取整,比如1->1,2、3->2,若是其它,則重新獲取一個3的random,當然要把1和2給去掉

——————————————
那不就更容易了,剩下的不需要重新獲取random了,直接就是3
————————————————————————

public static void main(String arg[]) {
System.out.println(getInt());
}
private static long getInt() {
long a = Math.round(Math.random() * 20);
if (a == 0 || a == 1) {
return 1;
} else if (a == 2) {
return 2;
} else {
return 3;
}
}

閱讀全文

與java概率計算相關的資料

熱點內容
主人公叫楊凡的小說 瀏覽:859
在船上做皮肉生意的電影 瀏覽:655
倫理電影飛在天上的船 瀏覽:224
求個網址能在線看 瀏覽:548
美國古埃及電影 瀏覽:77
韓國電影成人學院演員有誰 瀏覽:956
美國大胸電影 瀏覽:140
主角重生老北京的小說 瀏覽:199
邵氏100部恐怖影片 瀏覽:100
青春期2裡面的跳舞的歌 瀏覽:37
國產動作愛情片 瀏覽:420
韓國有部特種兵與護士的電影 瀏覽:662
《貪婪》中的日本女演員 瀏覽:477
男主得艾滋病的電影 瀏覽:807
罪孽船長泰國版在線觀看 瀏覽:194
外國電影一個黑男孩在深林 瀏覽:902
叔嫂不倫之戀電影 瀏覽:211
溫暖溫柔是哪部小說 瀏覽:204
穿越抗戰自立為軍閥的小說 瀏覽:601
韓國強殲電影有哪些 瀏覽:291