I need to generate a Exactly 6 digit random number.I have a method which generates 6 Digit Random#.But its generating repetative numbers which we don't want.Can some one suggest me a better technique which generates ramdom numbers which are unique(Its Ok if there are duplicates once in 1000 times)
You should be able to handle all your range issues in a single line:
int num = generator.nextInt(899999) + 100000;
The smallest random is 0 which gives 100000 for six digits. The largest random is 899999 which gives 999999.
Have you really found duplicates to be a problem? You could make something up to assure you go at least 1000 between duplicates, say a 3-digit random plus a 3-digit sequence number. The sequence part will repeat every 1000 and the random part will at least that often. Or you could keep a Set of the most recent 1000 Integers and loop until the one you generate is not in the set.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Using generator.nextInt(899999), the smallest possible return value is indeed 0 - but the largest is 899998. One less than the argument. I believe you actually want
which generates a value from 100000 to 999999.
More importantly though, the reason you're getting repeated values is because a new Random instance is being generated each time the method is called, and new Random() is using the current time as a seed. If you call this method twice less than 10 ms apart (depending on your system), you may well see the same system time - which means the "random" number generated from that seed will be the same. Instead, you want to reuse a single Random instance:
This should generate a new and different number, the vast majority of the time it's used. (You said it was OK if it repeated very rarely.) [ April 13, 2006: Message edited by: Jim Yingst ]
[Ken]: Besides, if it can't repeat then it's not really random.
I don't really buy that. We're talking about a uniform probability distribution of all integers from 100000 to 999999, compared to a uniform probability distribution of all integers from 100000 to 999999 excluding duplicates. These are just two among the infinite number of different probability distributions which might legitimately be called "random", even "truly random". (Ignoring the distinction between random and pseudorandom numbers, that's another issue.) People often overlook all but the simplest possible probability distributions, but that doesn't mean that only the simplest distributions are random.
In any event, if it's desireable to avoid duplicates entirely (using a uniform distribution of all non-duplicate values), it's not difficult:
This is similar to the algorithm used by Collections.shuffle(), but we shuffle only one element at a time, as needed. [ April 14, 2006: Message edited by: Jim Yingst ]
It has the best source of entropy for generating random numbers in Java as it's based on the JVM's thread timing, as opposed to java.util.Random or java.math.Random. Further, it's the recommended method to seed key generation in Java Cryptography.
Here's a code snippet to try:
[ April 14, 2006: Message edited by: M Anderson ]
Mike -- SCJP, SCWCD
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad: