Solved

UNIQUE random number

Posted on 2004-10-03
11
227 Views
Last Modified: 2010-03-31
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
Comment
Question by:jtcy
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12210652
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
 

Author Comment

by:jtcy
ID: 12210670
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
 
LVL 92

Expert Comment

by:objects
ID: 12210682
yep
0
 

Author Comment

by:jtcy
ID: 12210687
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
 
LVL 92

Expert Comment

by:objects
ID: 12210689
you need to use the Integer wrapper class so you can add them to the set.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jtcy
ID: 12210694
Do u mean...

mySet.add((Integer)randomNumber);


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12210776
mySet.add(new Integer(randomNumber));
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 20 total points
ID: 12211163
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12211183
That would have to be:


return generator.nextInt() ^ (int)System.currentTimeMillis();
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12211314
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12211415
>>the following is what java does automatically

Certainly doesn't in the version of sdk i'm using (1.5.0-beta2-b51)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now