Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. Your program must have method that returns the counts 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 count, to store the counts for the number of 0’s, 1’s, …, 9’s.

Recommended Answers

All 3 Replies

you can start by reading java books or online java tutorials on how to make a program

Your project description literally tells you exactly what you need to do. Create an array of ten integers. Every time you see a 0, increment array[0]. Every time you see a 1, increment array[1]. Etc.

I'm not helping you by doing this but I just wanted to have some fun so here's your solution:

public class RandomNumbers {
	
	public static void main(String[] args) {
		int[] frequency = new int[10];
		
		for(int i = 0; i < 100; i++){
			int randomNumber = generate();
			frequency[randomNumber]++;
		}
		
		printArray(frequency);
	}
	
	private static int generate(){
		return (int)(Math.random() * 10);
	}
	
	private static void printArray(int[] array){
		for(int i = 0, size = array.length; i < size; i++ ){
			System.out.println(i + " frequence -> " + array[i]);
		}
	}
	
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.