java - How to make not generate a number twice? -


this question has answer here:

in program using random generator.

random random = new random();  //how use int = random.nextint(numberneeded); 

the problem is, need find way each number gets generated once. guess not complete random....for example....when number 1 generated, dont want called again, instead call 1 of other numbers...like 2,3, or 4...then when 4 call can call 2 or 3..

then next time numbers shuffled , repeats process...

the numbers need range 4 16

it easier use collections.shuffle on list of elements want select from:

arraylist<integer> numbers = new arraylist<integer>(); (int i=4; i<=16; i++)     numbers.add(i); collections.shuffle(numbers); // numbers in random order , contain numbers 4 through 16 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -