IntelliJ Java IDE
The moose likes Java in General and the fly likes Randon number generation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Randon number generation" Watch "Randon number generation" New topic
Author

Randon number generation

Divya Venkatesh
Greenhorn

Joined: Nov 05, 2002
Posts: 28
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.


Divya
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Divya Venkatesh
Greenhorn

Joined: Nov 05, 2002
Posts: 28
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

Joined: Jan 30, 2002
Posts: 3451
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
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.


"I'm not back." - Bill Harding, Twister
Divya Venkatesh
Greenhorn

Joined: Nov 05, 2002
Posts: 28
Yeah,
thanks so Much
 
 
subject: Randon number generation
 
Threads others viewed
Conversion from String to int
how to clear the stack
MySql Random Alphanumeric number
Eclipse training
How can I fix the size of randomly generated numbers ?
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com