Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Purpose Use the random number generator and an array to keep track of how many t

ID: 3552950 • Letter: P

Question

Purpose

Use the random number generator and an array to keep track of how many times a number is generated.


Exercise

Write a program that generates 100 random numbers in the range of [0,9]. Use an array to keep track of the

frequency of each digit generated. Then display a frequency histogram to the console using asterisks to

represent the number of times the number was generated.

Printing format requirement:

? Number (number of times generated): one asterisks for every time generated. See example below.

0 (20): ********************

1 (7): *******

2 (12): ************

3 (7): *******

4 (11): ***********

5 (10): **********

6 (10): **********

7 (8): ********

8 (3): ***

9 (12): ************

For the example above, the number 0 was generated 20 times out of 100. The number 2 was generated 12

times and so on for every number.

import.java.util.Random;

Explanation / Answer

import java.util.Random;
public class RandomNumberGenerator

{

public static void main (String [] args )
{
    Random randomGenerator = new Random();
    int[] histogram= new int[10];
    for(int i=0;i<100;i++)
    {
        int randomInt=randomGenerator.nextInt(10);
        histogram[randomInt]=histogram[randomInt]+1;
    }
    for(int i=0;i<10;i++)
    {
        System.out.print(i+" ("+histogram[i]+"): ");
        int temp=histogram[i];
        for(int j=0;j<temp;j++)
        {
            System.out.print("*");
        }
        System.out.println("");
    }
}

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote