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
Post a Comment