Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Unique number generation in JdK1.4

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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: 22054
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Now does the number have to be 'unique' and 'random' or just 'unique'?
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic