Home arrow static arrow Java Programming [Archive] - Unique ID's in java - interessting theoretical problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Unique ID's in java - interessting theoretical problem
This topic has 16 replies on 2 pages.    1 | 2 | Next »

Posts:5
Registered: 2/3/00
Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 1:55 AM



 
Hey all,

I'm am trying to generate completely unique id's in java. These ID's should be provable unique within different processes and different systems. The machines IP address should not be assumed unique because, for example, two machine in a different subnet could have the same IP. The ethernet MAC address would be a better alternative (alltough also theoretically not unique). Regretably, the ethernet address cannot be acquired with java (as far as i know). I could use a central unique key generation server that all server have to conctact to get keys. This solution is not ideal because in some cases, easy network connection between the different machines is guaranteed.

Regardsn
Lennart
 

Posts:254
Registered: 8/22/01
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 2:09 AM (reply 1 of 16)



 
Instead of the key generation server you could give each instance of the program a unique key to start with. From that point you will only have to make sure the keys within each program are unique. Add the to key together and you will have an always unique key. This only work if you are able to control the distribution of the program or setup some kind of registration, but is that's possible this could be an solution.
 

Posts:5
Registered: 2/3/00
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 2:45 AM (reply 2 of 16)



 
This is indeed a solution. And cuurently the solution I use. But it is indeed the case that I don't have guaranteed control over the distribution. So automatic uniqueness would be nice. Also, from a theoretical point of view, i would like to see a solution to this problem.
 

Posts:2,238
Registered: 19/04/98
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 2:59 AM (reply 3 of 16)



 
Having a central unique id generator is the usual way to go about this.
There are various EJB-based solutions that simply increment a long every time a request for a new id is processed. Since the EJB guarantees the concurrent access is not a problem then a decent result is pretty much guaranteed.
Then again, a similar solution can be implemented by exposing a Servlet that uses an Oracle sequence.
If performance isn't so important you could write your own transactionally-safe sequence using the (shared) file-system and a robust file-locking policy.
As you've pointed out, all of these require that some network connectivity is provided. You could contact the server for every unique id or just when the software is installed to give the application a unique id from which it generates its own ids.

Hope this helps.
 

Posts:254
Registered: 8/22/01
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 4:32 AM (reply 4 of 16)



 
I think you need some kind of communication or somthing that is unique for each instance of the program. If communication, either through a central server or something like serialnumber distribution isn't possible you should find something that is unique for each instance of the program. But that will in most cases result in completely garuanteed to be unique. You might get the mac adress using jini, you could use things like IP adress, system time, host os, etc. and mix these together to get something that is very likely to be unique.
What do you want to use this for? If you can trust your users, to get a serial from some kind of webpage. But it's very likely your users are lazy and will recycle these numbers.
 

Posts:470
Registered: 5/10/02
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 4:46 AM (reply 5 of 16)



 
try to create an unique id which is composed from current time and a random part, maybe a additional hardware dependend component is possible.

Random rnd=new Random();

long uniqueId = ((System.currentTimeMillis()>>>16)<<16)+rnd.nextShort()
 

Posts:2,238
Registered: 19/04/98
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 4:54 AM (reply 6 of 16)



 
I'd call that a "probably unique" id.
Changes to the system clock, an unfortunate sequence of random numbers or concurrent user access are all possible reasons why this approach would fail very occasionally. It's certainly good enough for most stand-alone apps, though.
 

Posts:3,660
Registered: 10/24/98
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 5:50 AM (reply 7 of 16)



 
If you use a random number, time, ip, and the mac address to generate a string of sufficient length the odds of getting two strings the same are so close to zero they may as well be zero. Even two machines with the same ip are unlikely to get the same number given the time factor or the random number (which would be seeded by time probably).
 

Posts:10,967
Registered: 4/7/01
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 2:17 PM (reply 8 of 16)



 
A Universal Unique IDentifier (UUID) will probably do what you want. these are the strings that are normally encountered as "58f202ac-22cf-11d1-b12d-002035b29092"

I use a generator from Delphi named GUIDGen.exe (just search for it) tha has a gui interface. UUIDgen provides a programmatic interface. They are a proven item, Windows uses the heck out of them. Here's a llink that offers some explanation.

http://www.dsps.net/uuid.html
 

Posts:282
Registered: 6/4/01
Re: Unique ID's in java - interessting theoretical problem  
Jun 25, 2002 5:09 PM (reply 9 of 16)



 
well, what do you need the ID for? i mean just having it is not much good right? something has to use/ask for it right? so if you have the client/standalone generate it through all these wonderful ideas, then what? if you have a distribution run the exact same code then it can persist that id and use it later for whatever reason your doing this id thing in the first place.

how is it being distributed? you could combine the keygen with the download... put it in a servlet behind a web page... they click 'download app' and it asks the servlet which could write in the ID to the app source, compile, and download it ot the requestor...
or something like that.. then it persists it to a DB and now you can track who did what where, why, when and reference it later..

--jN
 

Posts:1
Registered: 6/24/04
Re: Unique ID's in java - interessting theoretical problem  
Jun 24, 2004 1:06 PM (reply 10 of 16)



 
I think what you are looking for is something like microsoft's concept of a GUID. A 128 bits of information that is unique in both time and space. I've read some posts on other forums that seem to indicate that this is theoritically impossible in Java due to limitations the VM blah blah blah. I'm not the kind of guy that takes no for an answer so I gave a pure java GUID my best shot. It may not be exactly unique in time and space, but its probably close enough. You could probably make it better with some kind of cryptographic Random number generator.

 /* *  Just a stand alone class that I posted to a forum * *  Copyright (C) 2004 Todd Clark quirkware0@yahoo.com * *  This library is free software; you can redistribute it and/or modify it *  under the terms of the GNU Lesser General Public License as published by the Free *  Software Foundation; either version 2.1 of the License, or (at your option) *  any later version. * *  This library is distributed in the hope that it will be useful, but *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *  or FITfNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License *  for more details. * *  You should have received a copy of the GNU Lesser General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * *  If you make changes or improvements to the this class I ask that you you  *  post them to a forum thread and email the improvements to quirkware0@yahoo.com  *  and that you leave my name intact in the header comments. * *  If you use this code in an application that you distribute. I require that you *  include my name in the credits of your application. */ public class PlatformIndependentGuidGen{  private static PlatformIndependentGuidGen _TheInstance = null;  private Random _RandomForSpace = null;  private Random _RandomForTime = null/**   * Note: the first time you call this function it is very expensive   * @return PlatformIndependentGuidGen the instace of the Guid generator   */  public static synchronized PlatformIndependentGuidGen getInstance()  {    if (_TheInstance == null)      _TheInstance = new PlatformIndependentGuidGen();     return _TheInstance;  }   /**   *   * @return String a new Guid   */  public String genNewGuid()  {    long uniqueInSpace = _RandomForSpace.nextLong();    long uniqueInTime = _RandomForTime.nextLong();     return Long.toHexString(uniqueInSpace) +        "-" + Long.toHexString(uniqueInTime);  }    private PlatformIndependentGuidGen()  {    long uniqueSpaceSeed = createUniqueSpaceSeed();    long uniqueTimeSeed = System.currentTimeMillis();     _RandomForSpace = new Random(uniqueSpaceSeed);    _RandomForTime = new Random(uniqueTimeSeed);  }   /**   * Does the best job it can to produce a number that is dependent on the machine   * on which this code is running   * @return long   */  private long createUniqueSpaceSeed()  {    String hashingString = ""; //Contains the conglomeration of as much machine or                               //or config dependant stuff as I could come up with     Properties systemProps = System.getProperties();    Enumeration systemPropKeys = systemProps.keys();    String systemPropKey = null;    Locale[] locales = Locale.getAvailableLocales();    int iCounter = 0;    Runtime runtime = Runtime.getRuntime();    InetAddress myAddress = null//Loop through all the system properties and add them to the hashingString    while(systemPropKeys.hasMoreElements())    {      systemPropKey = (String) systemPropKeys.nextElement();       //Append the system prop to the hashing string      hashingString = hashingString + " " + systemPropKey + " = " +          systemProps.getProperty(systemPropKey) + "\n"}     //Do a hardware dependent test and add the result to the hashingString    hashingString = hashingString + "Cycle Test = " +        cpuCycleTest() + "\n"//Loop through all the locales that are installed and add them to the hashingString    for (iCounter = 0; iCounter < locales.length; iCounter++)    {      hashingString = hashingString +          locales[iCounter].getCountry() + " " +          locales[iCounter].getDisplayCountry() + " " +          locales[iCounter].getDisplayName() + "\n";    }     //Get as much machine dependent stuff out of the runtime as    //you can and add them to the hashingString    hashingString = hashingString + "Available proc = " +        runtime.availableProcessors() + "\n";     hashingString = hashingString + "free memory = " +        runtime.freeMemory() + "\n";     hashingString = hashingString + "max memory = " +        runtime.maxMemory() + "\n";     hashingString = hashingString + "total memory = " +        runtime.totalMemory() + "\n"//try to get my IP address and add to the hashingString    try    {      myAddress = InetAddress.getLocalHost();      hashingString = hashingString + "my IP address = " +          myAddress.toString();    }    catch(Exception e)    {      //Do nothing    }     //System.out.println(hashingString);     return hashingString.hashCode();  }   /**   * how high can we count in 10 msec   * @return long   */  private long cpuCycleTest()  {    long returnValue = 0;    long startTime = System.currentTimeMillis();    long rightNow = System.currentTimeMillis();     //Do this for thirty millisec    while( (rightNow - startTime) < 10 &&           returnValue < (Long.MAX_VALUE - 10) )    {      returnValue++;      rightNow = System.currentTimeMillis();    }     return returnValue;  } }
 

Posts:7,258
Registered: 1/31/02
Re: Unique ID's in java - interessting theoretical problem  
Jun 24, 2004 1:20 PM (reply 11 of 16)



 
Excellent effort - but a couple years late on that one, don't you think?!
 

Posts:6,750
Registered: 1/25/04
Re: Unique ID's in java - interessting theoretical problem  
Jun 24, 2004 1:44 PM (reply 12 of 16)



 
Tomorrow it would have been a couple of years short. ;-)
 

Posts:3,055
Registered: 18/06/98
Re: Unique ID's in java - interessting theoretical problem  
Jun 24, 2004 1:50 PM (reply 13 of 16)



 
Cast your vote:
- Must old threads be set as "read-only" and closed to new postings?
 

Posts:6,750
Registered: 1/25/04
Re: Unique ID's in java - interessting theoretical problem  
Jun 24, 2004 1:55 PM (reply 14 of 16)



 
No.
 
This topic has 16 replies on 2 pages.    1 | 2 | Next »