File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unique number generation in JdK1.4

 
Shashidhar Yarabati
Ranch Hand
Posts: 175
  • 0
  • 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
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • 0
  • 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
  • 0
  • 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
 
Manuel Leiria
Ranch Hand
Posts: 171
  • 0
  • 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
  • 0
  • 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?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20399
47
Chrome Eclipse IDE Java Windows
  • 0
  • 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.
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now does the number have to be 'unique' and 'random' or just 'unique'?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic