In Jave Please Write a program that generates 100 random int
In Jave Please:
Write a program that generates 100 random integers between 0 and 9 and then reports the number of times each integer value was generated. Report also the integer value or values that were generated least and those which were generated the most.
An example run might look like this:
The count of values generated:
0: 15
1: 11
2: 5
3: 8
4: 10
5: 7
6: 15
7: 13
8: 5
9: 11
Values generated the least: 2, 8
Values generated the most: 0, 6
***** End of Program*****
Notes:
Be sure to set the upper limit for nine (9).
Use an array of ten integers -- call it counts for example -- to store the counts for the number of 0s, 1s,...9s.
Pass your array to a method that will generate and count the individual values. During testing, this method should also display the individual value when it is generated (see #5 below).
Use the printf() method on the report to align the integer counts on a right-justified boundary
You may want to test for validity by starting with a smaller number of randomly generated values and displaying each value as it is generated (and counted)
Solution
RandomNumberCount.java
import java.io.IOException;
import java.util.Random;
public class RandomNumberCount {
public static void main(String[] args) throws IOException {
Random r = new Random();
int counts[] = new int[10];
for(int i=0; i<100; i++){
counts[r.nextInt(10)]++;
}
generateCount(counts);
}
public static void generateCount(int counts[]){
int max = counts[0];
int maxIndex = 0;
int min = counts[0];
int minIndex = 0;
System.out.println(\"The count of values generated:\");
for(int i=0; i<counts.length; i++) {
System.out.printf(\"%d: %d\", i, counts[i]);
if(max < counts[i]){
max = counts[i];
maxIndex = i;
}
if(min > counts[i]){
min = counts[i];
minIndex = i;
}
System.out.println();
}
System.out.println(\"Values generated the least: \"+maxIndex+\", \"+max);
System.out.println(\"Values generated the most: \"+minIndex+\", \"+min);
}
}
Output:
The count of values generated:
0: 10
1: 9
2: 7
3: 15
4: 10
5: 14
6: 5
7: 5
8: 13
9: 12
Values generated the least: 3, 15
Values generated the most: 6, 5

