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.

Dani AI

Generated

Quick, practical clarification: the simplest correct approach is to encapsulate the counting in a method that returns an int[] of length 10, then call that method and print the results. is right to suggest using an array for tallies, and already posted a runnable example. The extra requirement here is that the counting logic should be packaged as a method that returns the counts so the logic is testable and reusable.

A concise, testable pattern is to write a method like countRandomDigits(int samples) that returns an int[10]. Keep printing and I/O out of that method so unit tests can call it directly. For deterministic tests, provide an overload that accepts a Random instance (so tests can pass a seeded RNG). Example usage and a minimal main are shown below.

import java.util.concurrent.ThreadLocalRandom;
import java.util.Arrays;

public class DigitCounter {
    public static int[] countRandomDigits(int samples) {
        int[] counts = new int[10];
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        for (int i = 0; i < samples; i++) {
            counts[rnd.nextInt(10)]++;
        }
        return counts;
    }

    public static void main(String[] args) {
        int[] counts = countRandomDigits(100);
        System.out.println(Arrays.toString(counts));
        System.out.println("Total = " + Arrays.stream(counts).sum());
    }
}

Troubleshooting tips and small cautions: use nextInt(10) (0..9) rather than an incorrect bound; avoid mixing counting and printing so you can assert sum == samples in tests; for repeatable unit tests inject a seeded Random instead of using ThreadLocalRandom. For API notes see ThreadLocalRandom.nextInt and Random.nextInt.

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.