Please do in a basic Java program and comment so that I understand what is going on.
Write a program that gives the statistics of a die being rolled 1000 times! Use a random number generator that picks only the number 1 through 6 and keep tally on each of each time the number is rolled. Store the number of times each number is rolled in the corresponding array index 1-6. For example, you will count the number of times the number 1 is rolled and store that value in index 1.
Note 1: You will not store anything in index 0.
Note 2:
import java.util.Random; //import Random class
Random rand = new Random(); //create instance of Random class
1 + rand.nextInt(10); //generates a random number between 1 and
10
Print the statistics at the end of the program.
Example output
1-563
2-105
3-52
4-201
5-70
6-9
import java.util.Random; public class DiceStatistics { public static void main(String[] args) { Random rand = new Random(); //create instance of Random class int[] counts = {0, 0, 0, 0, 0, 0, 0}; int num; for (int i = 0; i < 1000; i++) { num = 1 + rand.nextInt(6); // rolls a number from 1 to 6 counts[num]++; // increase the count for num } for (int i = 1; i <= 6; i++) { System.out.println(i + "-" + counts[i]); // display count for i } } }
Get Answers For Free
Most questions answered within 1 hours.