In: Computer Science
A dice has 6 face values: one, two, three, four, five, and six. If you roll a dice, you will see one of them with equal opportunity. Write a java program which rolls a dice 6000 times. The ideal case is that each face value will appear 1000 times. However, your program should give slightly different numbers.
The code is given below
import java.util.Random;
public class MyClass {
public static void main(String args[]) {
int[] arr=new int[6];
//to store the counts where 0 represents 1 on dice and 5 represents
6 on dice;
for(int i=0;i<6;i++)arr[i]=0;
Random r=new Random();
for(int i=0;i<6000;i++){
int rollValue=r.nextInt(6);
arr[rollValue]++;
}
for(int i=0;i<6;i++){
System.out.println("Count of "+(i+1)+" is "+arr[i]);
}
}
}
The output is