0

Need help getting populating array with random numbers 1-10 without using 0. - Create an array of 100 integers. I've tried int random = r.nextInt(High-Low) + Low; but that throws off the count of how many of each number.

What I need to do in my assignment:

  • Populate the array with random numbers ranging from 1 to 10. (not zero)
  • Determine what the average is of all the numbers in the array.
  • Count the occurrence of each of the ten numbers in the array of 100. Do by having a second array that's 10 integers in size and increment each element of the array based on the number of duplicates you find in your array of 100 integers.


package arrays;

import java.util.Arrays;
import java.util.Random;

public class Intergers {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Random r = new Random();

        // Create an array of 100 integers.
        int array[] = new int[100];

        int a = 0;

        // Populate the array with random numbers ranging from 1 to 10.
        while (a < 100)
        {
            int random = r.nextInt(10);
            array[a] = random;
            a++;
        }

        //calculate sum of all array elements
        int sum = 0;

         for(int i=0; i < array.length ; i++)
             sum = sum + array[i];

        //calculate average value
         double average = (double)sum/array.length;

        System.out.println("Array: " + Arrays.toString(array));
        // System.out.println("Sum: " + sum);
         //System.out.println("Array Length: " + array.length);
         System.out.println("Average value of array is: " + average);

         // Count the occurrence of each of the ten numbers in the array of 100
         int[] occurrences = new int[10];
         for (int b : array) {
                occurrences[b]++;
            }
         // System.out.println("Array: " + Arrays.toString(occurrences));



          System.out.println(1 + " appeared " + occurrences[0] + " times");
          System.out.println(2 + " appeared " + occurrences[1] + " times");
          System.out.println(3 + " appeared " + occurrences[2] + " times");
          System.out.println(4 + " appeared " + occurrences[3] + " times");
          System.out.println(5 + " appeared " + occurrences[4] + " times");
          System.out.println(6 + " appeared " + occurrences[5] + " times");
          System.out.println(7 + " appeared " + occurrences[6] + " times");
          System.out.println(8 + " appeared " + occurrences[7] + " times");
          System.out.println(9 + " appeared " + occurrences[8] + " times");
          System.out.println(10 + " appeared " + occurrences[9] + " times");




    }
}
Iamat8
  • 3,888
  • 9
  • 25
  • 35
Ryan Mills
  • 13
  • 4
  • "but that throws off the count of how many of each number" - in what way? (Bear in mind you either need `high - low + 1` or you need `high` to be exclusive...) – Jon Skeet Sep 29 '15 at 07:09
  • This is answered already: [http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java](http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java) – A.Panzer Sep 29 '15 at 07:15

2 Answers2

4
int random = r.nextInt(10);

would give you a pseudo-random int between 0 and 9. Just add 1 to get a range between 1 and 10 :

int random = r.nextInt(10) + 1;

You must also adjust your handling of the occurrences array to account for the fact that array indices start at 0 :

     int[] occurrences = new int[10];
     for (int b : array) {
         occurrences[b-1]++;
     }

     for (int i = 0; i < occurences.length; i++) {
         System.out.println(i+1 + " appeared " + occurrences[i] + " times");
     }
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Doing that gives me Thread [main] (Suspended (exception ArrayIndexOutOfBoundsException)) Intergers.main(String[]) line: 47 on 'occurrences[b]++;' and doing 'int High = 10; int Low = 1; int random = r.nextInt(High-Low) + Low;' gives me this http://pastebin.com/EvvpREq6 – Ryan Mills Sep 29 '15 at 07:21
  • @RyanMills That's a separate problem with the way you handle the `occurrences` array. See edit – Eran Sep 29 '15 at 07:23
0

I have an amateur way of doing this.

  1. create an arraylist containing 1-9
  2. create an array to store 100 integers
  3. iterate through the storage array and store the first item after a shuffle of the arraylist

implementation:

    ArrayList<Integer> a = new ArrayList<>();
    for (int i = 1; i < 10; i++) {
        a.add(i);
    }
    int[] store = new int[100];
    for (int i = 1; i < 100; i++) {
        Collections.shuffle(a);            
        store[i] = a.get(0);

    }
Mr Robbes
  • 49
  • 4