• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!