• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Unique Random Number

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to generate a UNIQUE and RANDOM number say for a transaction number and password generation. If I use the Random class like
Random ranObj = new Random();
int tranNumber = ranObj.nextInt();
Does this would always give a UNIQUE number? Coz I need to store this in the database table in a primary key column in identify the transaction.
Is there�s a better way which would guarantee the uniqueness across the application lifetime?
Thanks,
- VJ
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,
There is no way to guarantee that you will not get a duplicate. Random means that you can get any number. I have done a lot of tests for a recent Algorithms class. That said you could increase your range of numbers, but to be on the safe side you should do a lookup in the DB and see if the number already exists and then if it does fetch another random number.
When I say to seed it I mean like this:
randomInt(i, max);
Where i is some small number and max is a large number.
I hope this helps.
SAM
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,
Random numbers won't work for a unique identifier. Try this based on the system clock. It's still not guaranteed to give unique numbers if you reset the system time.

Another method would be to keep the next available number in a file and increment it each time it is used.
Finally, if you are using a database, can you just use an autonumber field in the table to create this automatically?
[ November 26, 2003: Message edited by: Tom Blough ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Random is tough, but sequential is easy. My current system uses a common scheme. The database has a "next available number" for each component. The component that inserts new records asks the key vendor "give me 1000 numbers". The key vendor might read the database and get the number 1, add 1000 and put 1001 back in the database, then return 1-1000 to the component. Now the component can do 1000 guaranteed unique inserts by just incrementing an in-memory counter before it has to go back to the key vendor. If the system crashes or shuts down for the night, some numbers might go unused but there is no risk of duplicates.
In the early 90s when I first used DB/2 the DBAs told us not to use sequential keys because physical disk storage routines got clogged up when you always add to the end of an index. So we used a timestamp with the digits reversed as the leading part of the key to slightly randomize the inserts. Our Oracle group says sequential is no longer a concern, but ya never know.
Finally, google on Java GUID generators and see how strongly they guarantee uniqueness.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic