Sri Jad

Ranch Hand

Posts: 49

posted 11 years ago

Hi Friends,

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)

Thanks In Advance

Jas

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)

Thanks In Advance

Jas

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 11 years ago

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.

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

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 11 years ago

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 ]

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 ]

"I'm not back." - Bill Harding, *Twister*

Ken Blair

Ranch Hand

Posts: 1078

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 11 years ago
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

I've been off by one for a long time.

True enough. Kinda like "If you don't find any bugs, it isn't testing" Well, more true than that.

Besides, if it can't repeat then it's not really random.

True enough. Kinda like "If you don't find any bugs, it isn't testing" Well, more true than that.

posted 11 years ago

If you pull vegetable out of the soup with covered eyes, without putting them back, you might get truly random results.

The probability to get a duplicate with putting back in a truly random way is

x = 0 + 1/(N-1) + 1/(N-2) + ... + 1/(N-999), N=900000

x=0.001110617123609 which is about 0.1%

You may generate a test and create 1000 Numbers a few million times, to see, whether the pseudo random numbers are similiar enough to truly random numbers.

Putting back duplicates seems a valid solution too, but using a sequence for a part of the number is not similar to randomnumbers at all.

The probability to get a duplicate with putting back in a truly random way is

x = 0 + 1/(N-1) + 1/(N-2) + ... + 1/(N-999), N=900000

x=0.001110617123609 which is about 0.1%

You may generate a test and create 1000 Numbers a few million times, to see, whether the pseudo random numbers are similiar enough to truly random numbers.

Putting back duplicates seems a valid solution too, but using a sequence for a part of the number is not similar to randomnumbers at all.

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 11 years ago

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 ]

**[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 ]

"I'm not back." - Bill Harding, *Twister*

M Anderson

Greenhorn

Posts: 25

posted 11 years ago

How about the java.security.SecureRandom class?

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 ]

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