Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# UNIQUE random number

Posted on 2004-10-03
Medium Priority
233 Views
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
Question by:jtcy
• 3
• 3
• 3
• +1

LVL 92

Expert Comment

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

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

ID: 12210682
yep
0

Author Comment

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();
}
}

-------------------------------------------------------------------
0

LVL 92

Expert Comment

ID: 12210689
you need to use the Integer wrapper class so you can add them to the set.
0

Author Comment

ID: 12210694
Do u mean...

0

LVL 86

Expert Comment

ID: 12210776
0

LVL 21

Accepted Solution

MogalManic earned 80 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

ID: 12211183
That would have to be:

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

LVL 21

Expert Comment

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

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

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET â€¦
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correctâ€¦
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompaâ€¦
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll