当前位置:首页>开发>正文

java程序设计,投骰子,求概率 java投掷骰子,求点数的出现概率

2023-07-07 00:33:12 互联网 未知 开发

 java程序设计,投骰子,求概率 java投掷骰子,求点数的出现概率

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

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

java投掷骰子,求点数的出现概率

很简单 。
int [] num = new int[3] //数组长度为 int diag=0 //统计出现5的次数
for(int i=0i>3i ){ //掷骰子三次 ,也就是三个骰子
随机数字 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)

求概率分布

好,我试着描述了一下从1 -5 的分布几率 以及变化:

In 1D6 case, the result is as following:
The probability of getting a 1 is 0.1The probability of getting a 2 is 0.1The probability of getting a 3 is 0.1The probability of getting a 4 is 0.1The probability of getting a 5 is 0.1The probability of getting a 6 is 0.1
In 2D6 case, the result is as following:
The probability of getting a 2 is 0.0The probability of getting a 3 is 0.0The probability of getting a 4 is 0.0The probability of getting a 5 is 0.1The probability of getting a 6 is 0.1The probability of getting a 7 is 0.1The probability of getting a 8 is 0.1The probability of getting a 9 is 0.1The probability of getting a 10 is 0.0The probability of getting a 11 is 0.0The probability of getting a 12 is 0.0
In 3D6 case, the result is as following:
The probability of getting a 3 is 0.00
The probability of getting a 4 is 0.0The probability of getting a 5 is 0.0The probability of getting a 6 is 0.0The probability of getting a 7 is 0.0The probability of getting a 8 is 0.The probability of getting a 9 is 0.1The probability of getting a 10 is 0.1The probability of getting a 11 is 0.1The probability of getting a 12 is 0.1The probability of getting a 13 is 0.The probability of getting a 14 is 0.0The probability of getting a 15 is 0.0The probability of getting a 16 is 0.0The probability of getting a 17 is 0.0The probability of getting a 18 is 0.00

In 4D6 case, the result is as following:
The probability of getting a 4 is 0.00
The probability of getting a 5 is 0.00
The probability of getting a 6 is 0.0The probability of getting a 7 is 0.0The probability of getting a 8 is 0.0The probability of getting a 9 is 0.0The probability of getting a 10 is 0.0The probability of getting a 11 is 0.0The probability of getting a 12 is 0.The probability of getting a 13 is 0.1The probability of getting a 14 is 0.1The probability of getting a 15 is 0.1The probability of getting a 16 is 0.The probability of getting a 17 is 0.0The probability of getting a 18 is 0.0The probability of getting a 19 is 0.0The probability of getting a 20 is 0.0The probability of getting a 21 is 0.0The probability of getting a 22 is 0.0The probability of getting a 23 is 0.00
The probability of getting a 24 is 0.00

In 5D6 case, the result is as following:
The probability of getting a 5 is 0.00
The probability of getting a 6 is 0.00
The probability of getting a 7 is 0.00
The probability of getting a 8 is 0.00
The probability of getting a 9 is 0.0The probability of getting a 10 is 0.0The probability of getting a 11 is 0.0The probability of getting a 12 is 0.0The probability of getting a 13 is 0.0The probability of getting a 14 is 0.0The probability of getting a 15 is 0.0The probability of getting a 16 is 0.0The probability of getting a 17 is 0.The probability of getting a 18 is 0.The probability of getting a 19 is 0.0The probability of getting a 20 is 0.0The probability of getting a 21 is 0.0The probability of getting a 22 is 0.0The probability of getting a 23 is 0.0The probability of getting a 24 is 0.0The probability of getting a 25 is 0.0The probability of getting a 26 is 0.0The probability of getting a 27 is 0.00
The probability of getting a 28 is 0.00
The probability of getting a 29 is 0.00
The probability of getting a 30 is 0.00

6d6上面的东西都和武器无缘了。。。不是长期拥有的东西也没必要去在意它到底出多少点吧? 大概规律就是中间多两边少,成钟型分布。

下面是程序:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package diceandprobability

import java.text.DecimalFormat
import java.text.NumberFormat
import java.util.ArrayList
import java.util.Iterator
import java.util.List
import java.util.Random

/**
*
* @author Yichuan
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

final int TOTAL_ROLL = 1000000
final int NUM_DICE = 2
final String PROMPT = "In " NUM_DICE "D6 case, the result is as following: "

Random ran = new Random()
List result = new ArrayList()
int num = 0
int count = 0

for(int i = 0 i < TOTAL_ROLL i ){

for(int j = 0 j < NUM_DICE j ){
num = ran.nextInt(6) 1
}
result.add(num)
num = 0
}

System.out.println(PROMPT)

for(int i = NUM_DICE i <= NUM_DICE * 6 i ){
Iterator iter = result.iterator()
while(iter.hasNext()){
if (iter.next() == i){
count
}
}

double amount = count / (TOTAL_ROLL 0.0)
NumberFormat format = new DecimalFormat("#0.00000")
System.out.println("The probability of getting a " i " is " format.format(amount))
count = 0
}
}
}