• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

UNIQUE random number

I have a constructor that needs to assign a unique random number to one of its variable everytime it is been constructed. Can someone tell me how can that be done? I mean, it has to be unique. hm~~~ Does normal random number generator guarantee unique number??
           
0
jtcy
Asked:
jtcy
  • 3
  • 3
  • 3
  • +1
1 Solution
 
objectsCommented:
no a random number generator will not guarantee uniquness.
you could store all the generated numbers in a Set, and check if it already exists using that (generating a new one if it did.
0
 
jtcyAuthor Commented:
so smth like...


------------------------------------


import java.util.Random;

public class Key
  {
     private int randomNumber;
     Set mySet = new Set();
   
     public Key()
      {
          Random generator = new Random();
          randomNumber = generator.nextInt();
          while (mySet.contains(randomNumber))
                randomNumber = generator.nextInt();
      }
}
               

-------------------------------------------------------------------
?

0
 
objectsCommented:
yep
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jtcyAuthor Commented:
oops. Should be:

------------------------------------


import java.util.Random;

public class Key
  {
     private int randomNumber;
     Set mySet = new Set();
   
     public Key()
      {
          Random generator = new Random();
          randomNumber = generator.nextInt();
          while (mySet.contains(randomNumber))
                randomNumber = generator.nextInt();
          mySet.add(randomNumber);
      }
}
               

-------------------------------------------------------------------
0
 
objectsCommented:
you need to use the Integer wrapper class so you can add them to the set.
0
 
jtcyAuthor Commented:
Do u mean...

mySet.add((Integer)randomNumber);


0
 
CEHJCommented:
mySet.add(new Integer(randomNumber));
0
 
MogalManicCommented:
I all you want is a number with a high probability that it is unique, do the following:

   private static Random generator=new Random();
   public int getKey()
   {
          return generator.nextInt() ^ System.currentTimeMillis()
   }
  The current time in millis will be unique as long as two threads don't call getKey at the same time.  If so, the random number should ensure uniqueness.
0
 
CEHJCommented:
That would have to be:


return generator.nextInt() ^ (int)System.currentTimeMillis();
0
 
MogalManicCommented:
no, Javas automatic conversion would be more correct  the following is what java does automatically:
    return (int) ((long) generator.nextInt() ^ System.currentTimeMillis());

OR if you did not want to drop bits
private static Random generator=new Random();
   public long getKey()
   {
         return generator.nextLong() ^ System.currentTimeMillis()
   }
0
 
CEHJCommented:
>>the following is what java does automatically

Certainly doesn't in the version of sdk i'm using (1.5.0-beta2-b51)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now