1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Pachacha [2.7K]
3 years ago
12

write a program that generates 100 random integrs between 0 and 9 and displays the count for each number. (Hint: Use (int) (math

.random() * 10) to generate a random integer between 0 and 9. Use an array of ten integers, say counts, to store the counts for the number of 0's , 1's,....,9's.)
Computers and Technology
1 answer:
Nataly_w [17]3 years ago
8 0

Answer: The java program to generate 100 random numbers and count their occurrence is shown below.

import java.util.Random;

import java.util.*;

public class MyClass {

   public static void main(String args[]) {

     int[]count = new int[10];

// temporarily stores the randomly generated number

     int num;

     for(int i=0; i<10; i++)

     {

// initialize count of all digits from 0 to 9 to zero

           count[i] = 0;

     }

     for(int i=0; i<100; i++)

     {

// generates 100 random numbers or integers from 0 through 9

           num = (int) (Math.random() * 10);

           for(int j=0; j<10; j++)

           {

// compare each randomly generated number with digits from 0 to 9 and increment their count accordingly

               if(num == j)

                   count[j] = count[j]+1;

           }

     }

     System.out.println("The count of randomly generated numbers is shown below.");

     for(int i=0; i<10; i++)

     {

           System.out.println("Count of "+i+" is "+ count[i]);

     }

   }

}

OUTPUT

The count of randomly generated numbers is shown below.

Count of 0 is 10

Count of 1 is 8

Count of 2 is 13

Count of 3 is 10

Count of 4 is 9

Count of 5 is 13

Count of 6 is 7

Count of 7 is 11

Count of 8 is 9

Count of 9 is 10

Explanation: This program declares an integer array of size 10 and initializes it to 0.

Random numbers are generated as shown. The number is randomly generated in an outer for loop with executes 100 times for 100 numbers.

(int) (Math.random() * 10)

The package java.util.Math is imported to serve the purpose.

The above code generates random numbers which can be floating numbers. The integer part is extracted using (int).

This yields the digits from 0 through 9.

For every occurrence of digits from 0 to 9, the count of the index of the respective digit is incremented.

To serve this purpose, inner for loop is implemented. The variable used in this loop is compared with the random number and index of count incremented accordingly. The inner for loop runs 10 times at the maximum, in order to match random number with 0 through 9.

Lastly, the occurrence of every digit from 0 to 9 is displayed.

You might be interested in
Explain why you do not need expensive equipment to take pictures or record video?
patriot [66]

Explanation:

Expensive equipment is not necessary because from a technical perspective as long as the device has a lens and a mic it should be able to take photos and videos.

4 0
3 years ago
Read 2 more answers
Which feature allows you to copy attributes of
avanturin [10]
I’m not sure if this is a more in depth question but this sounds like the feature “copy and paste”?
7 0
3 years ago
An icon in a document preset that looks like the play button on a YT video, what type of document is the preset meant to be used
zloy xaker [14]
The answer is film and video
8 0
3 years ago
Read 2 more answers
What is the purpose of quick access toolbar?
Flauer [41]
Answer:

Toolbar grants direct (quick) access to a set of desired commands in a toolbar that is always visible no matter which ribbon tab is selected.

Explanation:


Given
8 0
2 years ago
When creating a presentation you should use a blank as a starting point?
Lera25 [3.4K]
A blank presentation is recommended as the starting point when creating a PowerPoint presentation.  PowerPoint <span>lets you change the appearance, layout and content of your presentation at any time. Starting with a blank presentation lets you experiment more easily with the many features this program offers.</span>
6 0
3 years ago
Other questions:
  • Why should you log out when you finish an online session?
    9·1 answer
  • Computer virus is a<br> a. software<br> b. hardware<br> c. bacteria<br> d. none of these
    13·1 answer
  • What is meant by encapsulating semaphores? Bring out the need for it
    12·1 answer
  • Does functionality provided by the app play an important role in a decision to
    13·1 answer
  • Three of the most important jobs of security management are to ensure _____ are organized according to sensitivity, ensure that
    10·1 answer
  • Why does my hp computer keep freezing when i move it?
    10·1 answer
  • Even if you cannot afford it you should donate at least 5 of your earnings to charity true or false
    8·1 answer
  • Which type of error occurred in the following lines of code?
    13·1 answer
  • Mention any
    14·1 answer
  • ¿por que hay peligros en internet?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!