• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generate 6-Digit Random Number

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides, if it can't repeat then it's not really random.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been off by one for a long time.

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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic