Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating unique random numbers?  RSS feed

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i want to know how to generate unique random numbers.
I used this but this generates same numbers in a sequence.


Somebody help, please?
 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randomness does not imply nor require uniqueness. If you reduce the range of your random number generation to just three numbers (0, 1, or 2) then you'll quickly see how uniqueness is not a realistic expectation.

What you probably want to do is to take a list of as many unique numbers as you need and then shuffle it so it is in some "random" order. The collections API has a shuffle method defined in it: java.util.Collections.shuffle()
 
Khalid Habib
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Math.random();



OR just pass 50 in parameter then add 1

 
Junilu Lacar
Sheriff
Posts: 11138
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That still won't guarantee uniqueness.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan Crandego wrote:Hi, i want to know how to generate unique random numbers.

You can't because, by definition, a unique number can't be random.

What you can do - as has been described to you - is select from a list (or "bag") of distinct numbers in a random sequence. This mimics the way that lottery balls are picked from their big tumble dryer, or team/player matchups are selected for some knockout tournaments. Obviously, this approach has problems if the range of numbers you want to choose from is very large.

If the range of possible numbers is large compared to the number you want to pick, you can also repeat your nextInt(n) call while it returns a number that has already been selected; but this involves storing the numbers that have been selected in a way (usually a HashSet) that allows you to check this quickly, so it's not really suitable for drawing large numbers of unique values.

You can also generate a unique number, like a UID, which may have some randomness about it, but these "numbers" are usually extremely large.

So, you need to decide exactly what it is you want to do; but there is no "open-ended" way of simply generating unique random numbers.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:That still won't guarantee uniqueness.
Nor is Math#random a good way to select random ints. The method with a Random object is far better; in fact Math#random uses a Random object behind the scenes.

As people have told you, uniqueness is inconsistent with randomness. What you described is called random selection from a reducing population, not random numbers.
Another way to do it is to create a List and randomly remove numbers from it.Now use your Random object to remove one element at random. It says that Collections#shuffle runs in linear time, so that might be faster than my technique.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!