Random number generator over a range with no repeats
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Shahmil_Merchant
Posted On:   Thursday, August 29, 2002 08:50 AM

I have a method which generates random numbers and stores in into an array.I need to have random numbers as different as possible and covering over the entire range.These values are being stored in a histogram finally.But when I run the folowing code snippet i get a lot of the values being repeated and mostly within a certain region.(for e.g for values between range 0-10000 i get most of the random numbers between 2000-5000 and almost none towards the end).Here is the code (the commented lines indicates other options I have tried out. /* choose n random integers from 0 to max without repeating and store them in array randomnums*/ public void chooserandom(int[] randomnums, int max, int n) { int numgot; int i; int    More>>

I have a method which generates random numbers and stores in into an array.I need to have random numbers as different as possible and covering over the entire range.These values are being stored in a histogram finally.But when I run the folowing code snippet i get a lot of the values being repeated and mostly within a certain region.(for e.g for values between range 0-10000 i get most of the random numbers between 2000-5000 and almost none towards the end).Here is the code (the commented lines indicates other options I have tried out.

			
/* choose n random integers from 0 to max without repeating and store them in array randomnums*/
public void chooserandom(int[] randomnums, int max, int n) {
int numgot;
int i;
int newnum;
boolean repeat =false;

//Random generator = new Random(System.currentTimeMillis());
Random generator = new Random();
//byte seed[] = generator.generateSeed(n);
numgot = 0;
generator.setSeed(System.currentTimeMillis());
while (numgot < n) { /* numgot is the index of the last gotten item */
//generator.setSeed(System.currentTimeMillis());
newnum =generator.nextInt(max);
repeat = false;
for (i=0; i <=numgot; i++) { /* check got for repeats */
if( randomnums[i] == newnum) { /* it is a repeat */
repeat = true;
break;
}
}
if (repeat == false ) {
randomnums[numgot] = newnum;
numgot++;
}
}
}

When i did try setting a new seed in each iteration(dont know if that is the right approach) the program speed was extremely slow.

   <<Less

Re: Random number generator over a range with no repeats

Posted By:   Zac_Jacobson  
Posted On:   Thursday, August 29, 2002 02:52 PM

If possible, you'd be better off with next(int) than nextInt(int), as there is no filter that skews the randomness.. and by the way, filtering out duplicates also skews the randomness of your overall sample.

If that doesn't smooth out your histogram (you need a very large sample to be able to tell), try java.security.SecureRandom
which uses a stronger, but slower random number generation algorithm.

You are on the right track with seeding the generator to the time before you get in the loop, otherwise you will get the same random sequence every time you run the application.

It is not a good idea to re-seed for every iteration. Setting the seed just changes the starting point in the random (pseudo-random, actually) sequence, and you break the randomness by re-seeding every time.

About | Sitemap | Contact