Unique Random Number Generator in Java

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Does anyone know how to generate unique random numbers in Java? I need to generate a list of 10 random number below 50, and with no repeats. I feel like there must be something more elegant than checking each new number against the array, but I can't find it.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I don't know of any non-duplicating random number generators, but if I was checking and throwing away duplicates, I'd use a Map instead of an array. That way you get O(1) checking time instead of O(n).
 

Sunner

Elite Member
Oct 9, 1999
11,641
0
76
Originally posted by: joshsquall
Does anyone know how to generate unique random numbers in Java? I need to generate a list of 10 random number below 50, and with no repeats. I feel like there must be something more elegant than checking each new number against the array, but I can't find it.

Well, the numbers having be unique kinda defies the random part...
 
Sep 29, 2004
18,656
68
91
Originally posted by: joshsquall
Does anyone know how to generate unique random numbers in Java? I need to generate a list of 10 random number below 50, and with no repeats. I feel like there must be something more elegant than checking each new number against the array, but I can't find it.



If you are only doing 50 numbers, just use an array. the time to compare is insignificant.

Oh ... Map. Then use Map.contains(arg)! That would be nice indeed!
 

Avalon

Diamond Member
Jul 16, 2001
7,571
178
106
Hey guys, saw this thread, and my problem was a bit similar, so I didn't want to clutter the forum with another thread.

I'm trying to figure out how to generate random numbers within the range of -100 to 100. As for the size of the array, it is left up to user input. Could be 10, could be 1000. Here is the actual generating part of my code.

for (i = 0; i < nums; i++)
{


array = (int)(numgen.nextDouble() * MAX);
System.out.println(array);
i++;
array = (int)(numgen.nextDouble() * MIN);
System.out.println(array);
}

Where nums is the number of wanted elements for the array, MAX is preset to 100, and MIN is preset to -100. Currently, this will just generate a positive then negative number. I'd like to have it randomly pick a positive/negative number, not alternate. Duplicate numbers are fine. Any tips?
 

Soccer55

Golden Member
Jul 9, 2000
1,660
4
81
Originally posted by: Avalon
Where nums is the number of wanted elements for the array, MAX is preset to 100, and MIN is preset to -100. Currently, this will just generate a positive then negative number. I'd like to have it randomly pick a positive/negative number, not alternate. Duplicate numbers are fine. Any tips?

Here's an idea for the positive and negative thing. Why don't you just randomly generate a number and let that decide whether your number will be + or -. It's been a while since I've done any java, so I don't remember the exact syntax or commands, so hopefully you'll get the idea from what I'm writing.

For example:

for (i = 0; i < nums; i++)
{
sign = Math.random();

if( sign >= .5)
{
array = (int)(numgen.nextDouble() * MAX);
System.out.println(array);
i++;
}

else
{
array = (int)(numgen.nextDouble() * MIN);
System.out.println(array);
i++;
}

}

So if the variable sign is < .5, you'll generate a negative number and if it's >= .5, you'll generate a positive number.

-Tom
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: joshsquall
Does anyone know how to generate unique random numbers in Java? I need to generate a list of 10 random number below 50, and with no repeats. I feel like there must be something more elegant than checking each new number against the array, but I can't find it.

Shuffle an array of the first 50 integers and take the first 10?
Not particularly elegant either :p

How about this:
1. Build a list of the first 50 integers - call it x
2. generate a random integer j between 1 and the size of x
3. copy x[j] to your list of random numbers
4. delete x[j] from x decreasing the size of x by one
5. GOTO 2.
 

Avalon

Diamond Member
Jul 16, 2001
7,571
178
106
Did I mention I love you? :D

Thanks a bunch, works exactly as needed :)
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Avalon
Hey guys, saw this thread, and my problem was a bit similar, so I didn't want to clutter the forum with another thread.

I'm trying to figure out how to generate random numbers within the range of -100 to 100. As for the size of the array, it is left up to user input. Could be 10, could be 1000. Here is the actual generating part of my code.

for (i = 0; i < nums; i++)
{


array = (int)(numgen.nextDouble() * MAX);
System.out.println(array);
i++;
array = (int)(numgen.nextDouble() * MIN);
System.out.println(array);
}

Where nums is the number of wanted elements for the array, MAX is preset to 100, and MIN is preset to -100. Currently, this will just generate a positive then negative number. I'd like to have it randomly pick a positive/negative number, not alternate. Duplicate numbers are fine. Any tips?


Why not just generate the random numbers between 0 to 200 and then subtract 100?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Ok, so it returns a random double between 0 and 1 ... how does that help with the OPs problem? Generating a set of "random" integers in some range without duplicates.
 

Avalon

Diamond Member
Jul 16, 2001
7,571
178
106
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Is it THAT hard to use a more helpful tone? I had no knowledge of such a documentation. This is my first time using java.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Avalon
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Is it THAT hard to use a more helpful tone? I had no knowledge of such a documentation. This is my first time using java.

Well, Sun's java documentation pages show up in the first 3 results for nearly any java related search on google, they're not hard to find (go search for "java random" on google).

Regardless, you know about them now.
 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
Originally posted by: Armitage
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Ok, so it returns a random double between 0 and 1 ... how does that help with the OPs problem? Generating a set of "random" integers in some range without duplicates.

All you have to do is take the resulting random double and multiply it by your upper limit (50 in this case) and assign the result to an int, no?
Add that resulting value to a set (as somebody else mentioned, since these data structures automatically discard or overwrite duplicates) and repeat until the size of the set is 10.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: ahurtt
Originally posted by: Armitage
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Ok, so it returns a random double between 0 and 1 ... how does that help with the OPs problem? Generating a set of "random" integers in some range without duplicates.

All you have to do is take the resulting random double and multiply it by your upper limit (50 in this case) and assign the result to an int, no?
Add that resulting value to a set (as somebody else mentioned, since these data structures automatically discard or overwrite duplicates) and repeat until the size of the set is 10.

Yep, that should work - though depending on the number of unique random numbers you need relative to the range you can pick from, it may not be very efficient. Imagine an extreme case - picking 99 unique random numbers between 1 and 100. After you get 50 of them, you'll be discarding every other pick. You will need roughly 100 picks to get the last number. And internal to the set it will have to do a log(n) operation for every pick which could get expensive for large n. Of course both issues are trivial for the OP's 10 out of 50 problem statement.

Anyway, my point was that notfred's post was the documentation on random() which doesn't seem relavent to the OP's question.
 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
Originally posted by: Armitage
Originally posted by: ahurtt
Originally posted by: Armitage
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Ok, so it returns a random double between 0 and 1 ... how does that help with the OPs problem? Generating a set of "random" integers in some range without duplicates.

All you have to do is take the resulting random double and multiply it by your upper limit (50 in this case) and assign the result to an int, no?
Add that resulting value to a set (as somebody else mentioned, since these data structures automatically discard or overwrite duplicates) and repeat until the size of the set is 10.

Yep, that should work - though depending on the number of unique random numbers you need relative to the range you can pick from, it may not be very efficient. Imagine an extreme case - picking 99 unique random numbers between 1 and 100. After you get 50 of them, you'll be discarding every other pick. You will need roughly 100 picks to get the last number. And internal to the set it will have to do a log(n) operation for every pick which could get expensive for large n. Of course both issues are trivial for the OP's 10 out of 50 problem statement.

Anyway, my point was that notfred's post was the documentation on random() which doesn't seem relavent to the OP's question.

In the extreme case you mentioned, it would be more efficient to start with a collection of all 100 numbers, choose a random one, and discard it. Nobody would ever do something like pick 99 unique random numbers between 1 and 100.

Here's the code, I did your homework for you OP:

Set randoms = new HashSet();
while (randoms.size() < 10) {
int result = (int) (Math.random() * 49);
randoms.add(new Integer(result));
}


When done, the set randoms will contain 10 random numbers ranging from 0 to 49.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: ahurtt
Originally posted by: Armitage
Originally posted by: ahurtt
Originally posted by: Armitage
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Ok, so it returns a random double between 0 and 1 ... how does that help with the OPs problem? Generating a set of "random" integers in some range without duplicates.

All you have to do is take the resulting random double and multiply it by your upper limit (50 in this case) and assign the result to an int, no?
Add that resulting value to a set (as somebody else mentioned, since these data structures automatically discard or overwrite duplicates) and repeat until the size of the set is 10.

Yep, that should work - though depending on the number of unique random numbers you need relative to the range you can pick from, it may not be very efficient. Imagine an extreme case - picking 99 unique random numbers between 1 and 100. After you get 50 of them, you'll be discarding every other pick. You will need roughly 100 picks to get the last number. And internal to the set it will have to do a log(n) operation for every pick which could get expensive for large n. Of course both issues are trivial for the OP's 10 out of 50 problem statement.

Anyway, my point was that notfred's post was the documentation on random() which doesn't seem relavent to the OP's question.

In the extreme case you mentioned, it would be more efficient to start with a collection of all 100 numbers, choose a random one, and discard it.

Which is the solution I suggested earlier.

Nobody would ever do something like pick 99 unique random numbers between 1 and 100.

maybe not 99 of 100 but it's essentially shuffling a list - which I've certainly used before. In any case, my point is simply to understand where your algorithm may start to implode.

Here's the code, I did your homework for you OP:

Set randoms = new HashSet();
while (randoms.size() < 10) {
int result = (int) (Math.random() * 49);
randoms.add(new Integer(result));
}


When done, the set randoms will contain 10 random numbers ranging from 0 to 49.

 

ahurtt

Diamond Member
Feb 1, 2001
4,283
0
0
Originally posted by: Armitage
Originally posted by: ahurtt
Originally posted by: Armitage
Originally posted by: ahurtt
Originally posted by: Armitage
Originally posted by: notfred
Is it THAT hard to read the documentation? math.random() is not some super-secret advanced function.

Link

Ok, so it returns a random double between 0 and 1 ... how does that help with the OPs problem? Generating a set of "random" integers in some range without duplicates.

All you have to do is take the resulting random double and multiply it by your upper limit (50 in this case) and assign the result to an int, no?
Add that resulting value to a set (as somebody else mentioned, since these data structures automatically discard or overwrite duplicates) and repeat until the size of the set is 10.

Yep, that should work - though depending on the number of unique random numbers you need relative to the range you can pick from, it may not be very efficient. Imagine an extreme case - picking 99 unique random numbers between 1 and 100. After you get 50 of them, you'll be discarding every other pick. You will need roughly 100 picks to get the last number. And internal to the set it will have to do a log(n) operation for every pick which could get expensive for large n. Of course both issues are trivial for the OP's 10 out of 50 problem statement.

Anyway, my point was that notfred's post was the documentation on random() which doesn't seem relavent to the OP's question.

In the extreme case you mentioned, it would be more efficient to start with a collection of all 100 numbers, choose a random one, and discard it.

Which is the solution I suggested earlier.

Nobody would ever do something like pick 99 unique random numbers between 1 and 100.

maybe not 99 of 100 but it's essentially shuffling a list - which I've certainly used before. In any case, my point is simply to understand where your algorithm may start to implode.

Here's the code, I did your homework for you OP:

Set randoms = new HashSet();
while (randoms.size() < 10) {
int result = (int) (Math.random() * 49);
randoms.add(new Integer(result));
}


When done, the set randoms will contain 10 random numbers ranging from 0 to 49.

Obviously, as you should be able to tell from my previous post, I wouldn't use this algorithm to solve the problem you brought up. But that isn't the problem the OP wanted to solve.
 
Jun 6, 2005
194
0
0
java does not provide unique random number generator. it's pseudo random. try running the program a second time and you'll have the same results.

ntdz, there are many implementations of true random number generators. one way is the use of quantum mechanics which is thought of to revolve around true randomness