I can create random numbers from a range using
Random rand = new Random();
int num = rand.nextInt(10);
System.out.println("Generated Random Number between 0 to 10 is : " + num);
But if i need the next random number generated to be a part of the range minus the already generated one, will keeping the above statement in a loop will suffice?
Also I need to stop once i exhaust all the number from the range.
For eg,
The code gives me a random number between [0-10],
1st - 4 – range {[0-10]}
2nd - 9 – range {[0-10]-4}
3rd - 8 – range {[0-10]-4,9}
..
..
10th - 10 – range {[0-10]-[0-9]}
Will this function output from [Range]{this is my requirement} or (Range)? Or is there any better alternative solution?
The easiest way would probably be to:
Collections.shuffle()on that listSomething like:
You can even provide a random generator for the shuffling operation if the default doesn’t suit you.