dimanche 20 décembre 2015

Random number in array

I have that problem:

12 random integer numbers in the segment [-10;10] so that it has half of positive and half of negative numbers and has no zeros. The sequence has to be random.

for that I wrote that code:

        int[] integers = new int[12];
        Random r = new Random();
        for (int i = 0; i < integers.length; i++) {
          integers[i] = r.nextInt((10 - (-10) + 1) + (-10));
        }

I'm executing it using online compiler and it throws an error at random.

can anyone help me to solve that problem? I don't know how to write that half of it be negative and half positive. And that random number can't be 0




Aucun commentaire:

Enregistrer un commentaire