mardi 19 avril 2016

how to avoid repetition and zero's with java random function?

I have an array that contains 100 elements, I want to print out 20 random elements without repetition or having zero's i have successfully printed random 20 numbers out of 1000 but i can still get repetitions and zero's. any help?!

here is the code :-

import java.util.Random;


public class MyClass {
 public static void main(String args[]) {
 int[] persons = new int[1000];
 int[] person  = new int[20];
 Random random = new Random();

    for (int i = 0; i < person.length; i++)
        {
            person[i] = random.nextInt(persons.length);

            System.out.println(person[i]);
        }
}
}




Aucun commentaire:

Enregistrer un commentaire