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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Liutauras Vilda
• Paul Clapham
Sheriffs:
• paul wheaton
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Piet Souris
Bartenders:
• Mike London

# generating unique numbers

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
how can i generate unique random numbers between a range, say, 0 to 20.

Author
Posts: 121
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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: 155
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
See this for detailed discussion.

Dirk Schreckmann
Sheriff
Posts: 7023
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 18671
• Number of slices to send:
Optional 'thank-you' note:
Welcome back from your vacation, Dirk.

Greenhorn
Posts: 3
• Number of slices to send:
Optional 'thank-you' note:
To generate "Genuine random numbers"