How to do a random-and-unique generator?
Posted
by javaLearner.java
on Stack Overflow
See other posts from Stack Overflow
or by javaLearner.java
Published on 2010-05-11T16:32:25Z
Indexed on
2010/05/11
16:34 UTC
Read the original article
Hit count: 144
I already wrote a random generator which take arguments a and b, where a is minimun and b is maximum value, like this randomGenerator(int a, int b)
What I want to do next is: Using a loop, then generate 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 the loop, there is a high % that same number will appear more than once. Any idea how to do it?
My own way is:
while(int i <= 8){
randomGenerator(a,b);
// if value is not in array, then insert into array
}
I am stuck at the comment part. Is there any way to check if a variable is exists in an array?
© Stack Overflow or respective owner