John D. Rockefeller
John D. Rockefeller

Reputation: 11

Storing Random Numbers in an Array

I am a bit confused on how I would take the randomly generated number in a range from my program, store that into an array, and read and print out from the array how many times that the number was generated.

For the random import I am using java.util.concurrent.ThreadLocalRandom;

public static void main(String[] args) { 
    char quitOption = 'q'; 
    char continueOption = 'c';
    char input;
    int[] myArray;
    Scanner console = new Scanner(System.in);

    do {
        int roll = ThreadLocalRandom.current().nextInt(1, 6);
        System.out.println("Roll is " + roll);
        System.out.println("Enter c to continue or enter q to quit ");
        input = console.nextLine().charAt(0);

        if (input == continueOption || input == 'C') {
            roll = ThreadLocalRandom.current().nextInt(1, 6);
            System.out.println("Roll is " + roll);
            System.out.println("Enter c to continue or enter q to quit ");
            input = console.nextLine().charAt(0);
        } else if (input == quitOption || input == 'Q') {
            System.exit(0);
        }
    } while (continueOption == 'c' || continueOption == 'C');
}

Upvotes: 0

Views: 280

Answers (2)

GBlodgett
GBlodgett

Reputation: 12819

You must figure out how to overcome two problems:

  • Figure out how many rolls there will be, as Array's are fixed sized

  • Count how many time a number is rolled

You could use a List, and then use built in methods such as Collections.frequency, or if you are confined to an Array, check to make sure that adding another number will not be out of bounds, (And if it will be then copying it to a new Array) and then iterating over the Array and counting how many times each number occurs.

However, we know the range of numbers that will occur. So why not initialize an Array with six elements, and let 0 be 1, 1 be 2, and so on. Then every time that number is rolled, we increment the index of the respective number. So something like:

int roll = ThreadLocalRandom.current().nextInt(1, 6);
arr[roll -1]++;

So if a two is rolled, we will add one to the 1th index:

[0, 1, 0, 0, 0, 0]

And so on. Then when you need to count the index its a simple loop:

for(int i = 0; i < arr.length; i++) {
   System.out.println(i + 1 + " occurs: " + arr[i] + " times");
}

Also you are over complicating your loop. It can be simplified to:

char input;
int[] myArray = new int[6];
Scanner console = new Scanner(System.in);
do {
    int roll = ThreadLocalRandom.current().nextInt(1, 6);
    System.out.println("Roll is " + roll);
    myArray[roll -1]++;
    System.out.println("Enter c to continue or enter q to quit ");
    input = console.nextLine().charAt(0);

} while (input == 'c' || input == 'C');
for(int i = 0; i < myArray.length; i++ ) {
    System.out.println(i + 1 + " occurs: " + myArray[i] + " times");
}

Sample run:

Roll is 4
Enter c to continue or enter q to quit 
c
Roll is 1
Enter c to continue or enter q to quit 
c
Roll is 3
Enter c to continue or enter q to quit 
c
Roll is 3
Enter c to continue or enter q to quit 
c
Roll is 1
Enter c to continue or enter q to quit 
c
Roll is 1
Enter c to continue or enter q to quit 
q
1 occurs: 3 times
2 occurs: 0 times
3 occurs: 2 times
4 occurs: 1 times
5 occurs: 0 times
6 occurs: 0 times

Upvotes: 1

Chunker
Chunker

Reputation: 223

I would use a HashMap<Integer, Integer> lets call it rollMap.

Each time you roll you get int currentRoll = randomRoll().

If I were you, I would then say:

if(rollMap.containsKey(currentRoll)){
    rollMap.put(currentRoll, rollMap.get(currentRoll) + 1);
}else{
    rollMap.put(currentRoll, 1);
}

You can then get how many times each number was rolled by saying:

System.out.println(rollMap.get(<rollid>));

Upvotes: 2

Related Questions