java - ArrayList contains -
i don't understand what's happening, if explain me great.
so, here's code:
public static arraylist<integer> numbers = new arraylist<integer>(); public static void main(string[] args){ for(int =0; != 90; i++){ system.out.println(generate()); } } public static int generate(){ random random = new random(); int rand = random.nextint(89)+1; while(numbers.contains(rand)){ //<---here seems problem rand = random.nextint(89)+1; system.out.println("number: " + rand + " exists!"); } numbers.add(rand); return rand; }
i writing program generates random number 0-90, each of different last. unfortunately, seems while loop returns true.
you can using collections shuffle
public static arraylist<integer> numbers = new arraylist<integer>(); for(int =1; <= 90; i++){ number.add(i) } collections.shuffle(numbers); // @ point number shuffled.
read shuffle.
Comments
Post a Comment