Need help to write this java program.... For this lab, feel free to have a neigh
ID: 3821922 • Letter: N
Question
Need help to write this java program....
For this lab, feel free to have a neighbor help decipher a compilation error message you get.
Generate 100,000,000 random numbers in the range 0..9,999. For each number generated, determine the last digit and count the occurrence of the last digit. [ you are not holding the 100,000,000 in memory – inside the loop generate #, determine last digit, increment appropriate counter ]
You can either use 10 counters, or you can use 1 array of 10 ints ( 0..9 ) for the counter.
After all of the numbers are generated, report the counts by digit.
Sample Run
Last
Digit Count
0 9,995,956
1 10,003,864
2 9,997,903
3 9,997,796
4 9,999,679
5 10,000,971
6 10,006,986
7 9,999,226
8 9,999,525
9 9,998,094
Press any key to continue . . .
Explanation / Answer
import java.util.*;
public class RandomNum
{
public static void main(String a[])
{
int freq[]= new int[10];
Random r= new Random();
int n;
int last;
for(long i=0; i<100000000; i++ )
{
n = r.nextInt((9999) + 1);
last = n%10;
freq[last]++;
}
for(int i=0;i<10;i++)
{
System.out.println(i+": "+freq[i]);
}
}
}