Write a complete program in a single class called RandomCount that works as foll
ID: 3633020 • Letter: W
Question
Write a complete program in a single class called RandomCount that works as follows.
Your code should make use of a Random object from the Random class to generate 10000 integers starting with 1 and going up to and including 10, at random. Using an array, count the number of times each number from 1 to 10 is produced. After the numbers have been tallied your program should print out, in a column, the numbers 1 through 10, along with how many times each has been generated. Typical program output is given below.
1 1002
2 989
3 990
4 1026
5 990
6 1001
7 980
8 121
9 102
10 999
Explanation / Answer
please rate - thanks
import java.util.*;
public class RandomCount
{public static void main(String[] args)
{int i,n;
int [] nums =new int[10];
Random r=new Random();
for(i=0;i<10;i++)
nums[i]=0;
for(i=0;i<10000;i++)
{n=r.nextInt(10)+1;
nums[n-1]++;
}
System.out.println("N Count");
for(i=0;i<10;i++)
System.out.println((i+1)+" "+nums[i]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.