I already wrote a random generator that takes a and b arguments, where a is the minimum and b is the maximum value, like this randomGenerator (int a, int b)
Next I want to: use a loop, and then generate a unique number from a to b. Example:
I want to have 8 unique numbers, int a = 1; int b = 10; int value;
If I do a loop, there is a high% that the same number will appear more than once. Any idea how to do this?
My own way:
while(int i <= 8){ randomGenerator(a,b); // if value is not in array, then insert into array }
I am stuck in the comment part. Is there a way to check if a variable exists in an array?
Edit, based on nailxx's answer, I understand:
take a list from a to b (if you follow my example, 1-10)
"shuffle" it
take the first 8 items. Is that what you mean?
Is there a "shuffle" function in the java world or do I need to create my own?
source share