• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Randon number generation

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone give me a method to generate Random Numbers from say 1 to 100 ensuring that the same number does not repeat again?
Any ideas.
I know how to generate them but I have no idea to ensure they dont repeat.
thanks a lot in advance.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,
Well you can keep up with what's already been generated using a somewhat naive approach like this:

Once the set has almost all the possibilities, the while loop in the nextInt() method will have to go thru many iterations to gen a unique random number. I'm no expert on pseudo-random number generation, but this may cause a non random pattern to appear. That may or may not be a problem depending on your use.
Hope this helps,
Michael Morris
 
Divya Venkatesh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Thanks a lot micheal.But I dont Quite Understand the logic.
Can u explain how it manages to find non repeating numbers??
thanks a lot.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,
We keep up with all the previously generated numbers in Set ints. A Set by definition does not allow for duplicate entries so all we have to do is check if the value has already been added to it and if it has continue looping until we generate a value that is not in it. This is what I meant by a naive approach since it is a grossly inefficient way to accomplish our task. Note that you cannot add primitives like int to a Java Set so we create an Integer object which is Java's wrapper for int. The crux of all this is in the following:

The line if (! ints.contains(i)) checks that the newly generated random int is not already in the Set. If this conditional evaluates true, we add the Integer to the Set, make our return value (r) equal to the intValue() and break out of the loop; otherwise we iterate the loop again.
Note that when our nextInt() returns -1, then all the possibilities have been generated. At this point the instantiation of UniqueRandomInt is no longer useful and you will have to construct a new one if need be. Of course you could add a clear() method to restart the process and in it call ints.clear() to remove everything from the Set. You could also add a method called setRange(int range), which would set high to range (this.high = range).
Hope this is helpful,
Michael Morris
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See previous threads here and here. The best combination of simpicity and efficiency is probably to create a List of Integers for all the numbers in your desired range, and then use Collections.shuffle() to scramble them completely. To improve speed you can adapt shuffle() to work on an array of ints rather than a List of Integers - but the idea is the same.
 
Divya Venkatesh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah,
thanks so Much
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic