Hi all,
I have an array of N items, and I'd like to select "randomly" from it. What's the best way to do this? (That the selection sequence be highly pseudorandom is not important. I simply do not want to have a selection routine that's like "go in increasing order" or "take every 10th element (wrapping around w/o repeats)" or something like that.)
The most obvious answer seems to be: rand() % N, which will give me an integer between 0 and N-1, inclusive.
But from what [little] I understand about prng's, relying on the last bits of a pseudorandom # can be dangerous. But I don't know how bad it is. Like I don't care if the sequence of N numbers is very predictable. I do care if the sequence of N numbers lie mostly in the range say 10 to 20.
Lastly, computational complexity isn't that important, but it should not be more than a handful of simple arithmetic operations and a single call to rand(). Also it is CRITICAL that the method -always- return a number in the specified range--i.e. I don't want to deal with overflow/underflow, division by 0, etc. Avoiding denormalized numbers would also be good.
Thanks,
-Eric
I have an array of N items, and I'd like to select "randomly" from it. What's the best way to do this? (That the selection sequence be highly pseudorandom is not important. I simply do not want to have a selection routine that's like "go in increasing order" or "take every 10th element (wrapping around w/o repeats)" or something like that.)
The most obvious answer seems to be: rand() % N, which will give me an integer between 0 and N-1, inclusive.
But from what [little] I understand about prng's, relying on the last bits of a pseudorandom # can be dangerous. But I don't know how bad it is. Like I don't care if the sequence of N numbers is very predictable. I do care if the sequence of N numbers lie mostly in the range say 10 to 20.
Lastly, computational complexity isn't that important, but it should not be more than a handful of simple arithmetic operations and a single call to rand(). Also it is CRITICAL that the method -always- return a number in the specified range--i.e. I don't want to deal with overflow/underflow, division by 0, etc. Avoiding denormalized numbers would also be good.
Thanks,
-Eric
