Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

generating unique numbers  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i generate unique random numbers between a range, say, 0 to 20.
 
Author
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello !
I'm not sure, but I think you mean a sequence of numbers in an arbitrary order (a so called permutation).
A possibility to do so is to create an int-array with the numbers in the correct order. Something like

After that you could create a loop (e.g. 2*n iterations) and in each iteration you take to random numbers beetween 0 and n-1 and swap the two corresponding elements in the numbers-array. Use the Random-class in java.util for getting the random-numbers (method nextInt()).
I hope that helped and I understood your problem ...
Greetings from Hamburg, Stefan
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Random only gives a pseudo random sequence, you could try seeding the Random object with the current time in milliseconds. Something like this should work:
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a series of random numbers that will not contain identicals try
(1) creating a int array to hold your random numbers
(2) As many times as you need numbers:
(a) generate random numbers in the desired range
(b) test each number against all already stored in the array - this would likely be a method like "isANewOne" that returns a boolean. If isANewOne returns true you add the new random to the array and count that as one more successful find, otherwise you generate & test a new random.
(3)Once your number of successful finds equals the number of unique randoms needed, you're done.
(4)Remember to testy that the request is possible - for example, there's only 40 unique randoms between 1 and 40.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urooj,
I'd recommend that you take a look at a past conversation on this topic in Thomas Paul's Challenge: Optimum way to randomize 1,000,000 numbers.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this for detailed discussion.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Dermot Curley:
you could try seeding the Random object with the current time in milliseconds.


The default constructor of Random already does this.
http://java.sun.com/j2se/1.4/docs/api/java/util/Random.html
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome back from your vacation, Dirk.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To generate "Genuine random numbers"
read
http://fourmilab.ch/hotbits/
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!