-2

I am having trouble placing the random generator in the array, how can I get #20 random numbers from 0-9 in the array? Then you count the occurrences of those numbers.

import java.util.Random;

public class CountDigits {

public static void main(String[] args) {



    Random digit = new Random(); 

    int Random[] = new int [20];

    int Digits[] = {0,1,2,3,4,5,6,7,8,9}; 

    int Count [] = new int [10]; 


    for ( int i = 0; i < Digits.length; i++) {


        for( int j = 0; j < Random.length; j++) {

            if ( Digits [i] == Random [j] ) 
                Count[i]++; 
        }

    }


    for ( int i = 0; i < Count.length; i++) {

        if ( Count[i] == 1) 
            System.out.printf(" %d  occurs  1 time " , Digits[i] ); 

        else
            System.out.printf("%d occurs %d times" , Digits[i], Count[i]); 

    }

result so far:: 0 occurs 20 times1 occurs 0 times2 occurs 0 times3 occurs 0 times4 occurs 0 times5 occurs 0 times6 occurs 0 times7 occurs 0 times8 occurs 0 times9 occurs 0 times

sandskrip
  • 15
  • 1
  • 1
  • 1
    "How to fill array with random numbers limited to 0-20" | "how can I get #20 random numbers from 0-9 in the array" - Which is it? – Jacob G. Apr 22 '19 at 23:29
  • *"I am having trouble placing the random generator in the array"* Why would you want to? You only need one random generator, so why put *it* in an array? You'd want to put the generated random *numbers* in the array, so to begin with, change `int Random[] = new int [20];` to `int[] numbers = new int[20];` – Andreas Apr 22 '19 at 23:35
  • Parts duplicate of [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/q/363681/5221149) – Andreas Apr 22 '19 at 23:38
  • In addition to the various other comments, you need to pay attention to Java style conventions. 1) The identifier for a variable should *never* start with an uppercase letter. 2) The name for an identifier should reflect what the variable is *used for*. So for example, calling your random number generator `digit` is wrong. It is not a digit. It is a random number generator. – Stephen C Apr 23 '19 at 02:44

3 Answers3

0

You forgot to assign random numbers to the array elements.this answer to see how random numbers are generated.

You need to call Random object's nextInt(an integer) method. If you give 25 to it, it will return a random integer between 0-24 inclusive.

Example Usage:

Random rand = new Random();
int random_int = rand.nextInt(50); // --------> Random integer 0-49

And the full code:

import java.util.Random;

public class Main {

    public static void main(String[] args) {


        Random digit = new Random();

        int Random[] = new int[20];

        for (int x=0;x<20;x++) {
            Random[x] = digit.nextInt(10);
        }

        int Digits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

        int Count[] = new int[10];


        for (int i = 0; i < Digits.length; i++) {


            for (int j = 0; j < Random.length; j++) {

                if (Digits[i] == Random[j])
                    Count[i]++;
            }

        }


        for (int i = 0; i < Count.length; i++) {

            if (Count[i] == 1)
                System.out.printf(" %d  occurs  1 time ", Digits[i]);

            else
                System.out.printf("%d occurs %d times", Digits[i], Count[i]);

        }
    }
}

Outputs :

0 occurs 2 times1 occurs 2 times 2  occurs  1 time 3 occurs 3 times4 occurs 6 times 5  occurs  1 time 6 occurs 2 times7 occurs 0 times 8  occurs  1 time 9 occurs 2 times

You can print the random numbers like this

 for (int x=0;x<20;x++) {
        System.out.println(Random[x]);
    }
srknzl
  • 776
  • 5
  • 13
0

You need to actually get a Random number, and get within the range. The Java Random will provide the necessary capability, and includes a .nextInt(int bound) that returns between 0 and bound:

public int nextInt(int bound)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

So something like:

Random rnd = new Random();

int num = rnd.nextInt(10);  // returns between 0 and 9

Given that, you have two other questions:
- Generating 20 numbers
- Counting

Knowing that the number of possible entries is between 0 and 9, it is easy to use an array to hold the counts.

int[] counts = new int[10];  // holds between 0 and 9
for (int i = 0; i < 20; ++i) {
  int num = rnd.nextInt(10);
  counts[num]++;
}

The output of the counts array will give the count of the number of times a given number between 0 and 9 was randomly generated.

See an example here

KevinO
  • 4,303
  • 4
  • 27
  • 36
0

You can try this:

Create an array of int with length 20, then use a simple for loop where you are gonna generate a random number and them put it inside the array

int[] random = new int[20];

for(int i=0;i<20;i++){

    int r = Math.floor(Math.random()*9+1);
    random[i]=r;
}
icortazar
  • 110
  • 1
  • 15