aspose file tools*
The moose likes Java in General and the fly likes Unique number generation in JdK1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unique number generation in JdK1.4" Watch "Unique number generation in JdK1.4" New topic
Author

Unique number generation in JdK1.4

Shashidhar Yarabati
Ranch Hand

Joined: Jun 17, 2007
Posts: 175
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

Joined: May 27, 2008
Posts: 225

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

Joined: Jun 17, 2007
Posts: 175
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

Joined: Jul 13, 2007
Posts: 171
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.


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Shashidhar Yarabati
Ranch Hand

Joined: Jun 17, 2007
Posts: 175
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

Joined: Aug 16, 2005
Posts: 14146
    
  18

You cannot make Math.random() return unique numbers by using the synchronized keyword. Where did you hear this?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 562
    
    7

Now does the number have to be 'unique' and 'random' or just 'unique'?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unique number generation in JdK1.4