In: Computer Science
Assignment 7 Preparation |
This assignment will focus on the use of loops, random numbers, named constants, and arrays. |
Assignment 7 |
Assignment 7 Submission |
Grading criteria: |
Thanks for the question, here is the Java class
==========================================================================
import java.util.Random;
public class RandomDistributionCheck {
public static void
main(String[] args) {
Random random =
new Random();
final
int ITERATION_COUNT = 10000000; // constant
final
int UNIQUE_NUMBERS = 20; // constant
int
numberOccurences[] = new int[UNIQUE_NUMBERS]; //
array to store the count
for
(int count = 1; count <= ITERATION_COUNT;
count++)
numberOccurences[random.nextInt(UNIQUE_NUMBERS)]++;
// print header
System.out.printf("%-8s %18s
%22s\n",
"Number", "Occurrences",
"Percentage(%)");
for
(int index = 0; index < UNIQUE_NUMBERS;
index++) {
// print the number the occurence count and percentage
System.out.printf("%-8d %15d
%18.2f\n", index,
numberOccurences[index], numberOccurences[index] * 100.0 /
ITERATION_COUNT);
}
}
}
================================================================