• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Unique number generation in JdK1.4

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All

My guys here managed to get the unique number wiht Math.random(). Till now (around 3 years) it is working fine, now we got an issue which shows two sessions (not sessionId, we are using this unique number for other reasons) are running with same unique number.

The problem here is, I can't use UUID class because my application is running under Jdk1.4.

Can any help me to impelement the best solution here...!!

THanks in Advance
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java 5 UUID is essentially a 122-bit random number, compared to Math.random() which is a 53-bit random number, so collisions are only much less likely with a UUID, not impossible.

That said, you could generate a 128-bit random number like this:If you want to avoid collisions altogether, you'll have to test for them before returning the number.
 
Shashidhar Yarabati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will this really guarantee me to get unique number?

One thing I can't do is I can't keep track of generated numbers to check.

Please help me to think..........! I am running out of time
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shashidhar Yarabati:
Will this really guarantee me to get unique number?

One thing I can't do is I can't keep track of generated numbers to check.

Please help me to think..........! I am running out of time



There's no way this will guarantee the uniqueness because...it's a random number!
I can't see any other way but keeping track of the generated numbers.
 
Shashidhar Yarabati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does any one now how to add synchronize keyword to Math.random?

My code is:

Integer globalTicketID = null;
do {
globalTicketID = new Integer(Math.round((float) Math.random() * 10000000));
} while (mGlobalTkt.containsKey(globalTicketID));


I hear that we can fix by adding a single synchronize keyword.

I didn't see that in API

Can any one know how to do this?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot make Math.random() return unique numbers by using the synchronized keyword. Where did you hear this?
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside your JDK (5.0 or 6) folder there is a file called src.zip. Inside it you can find the source code of java.util.UUID. You can use it as a hint on how to implement something similar.

I'm saying something similar, and not copy the file, because of the copyrights this file has.
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now does the number have to be 'unique' and 'random' or just 'unique'?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic