in reply to Generating 0 .. N Randomly and Efficiently

I have the feeling that searching forward or backward won't result in a well shuffled result; I'd think you'd tend to end up with some ranges of numbers clumping toward the end and some toward the beginning. Just a feeling, though...

One alternative is to do random selection (repeating until successful) for the first X percent of N, then switching to scanning through the whole bitmap until you find the rand(number-remaining)'th remaining number. Some benchmarks would be needed to pick a good X; I'd guess 90%.

  • Comment on Re: Generating 0 .. N Randomly and Efficiently

Replies are listed 'Best First'.
Re^2: Generating 0 .. N Randomly and Efficiently
by Limbic~Region (Chancellor) on Oct 19, 2004 at 17:20 UTC
    ysth,
    I haven't spent a lot of time analyzing the distribution frequency, but this implementation does have the possibility of clumping as both you and bart indicate. This, however, is only one of dozens of variations I tried over the last couple of days and others were better at being more statistically uniform.

    I was hoping someone else would give it a try to see what they could come up with as there are far smarter monks here than myself. I know it seems silly. The answer without restrictions is obvious, assign to an array and shuffle. It just seems to be that there should be more efficient ways to reduce the search space, determine an unchosen bit randomly without a lot of work cycles, etc. It is obvious to me I need to learn more about bitstrings and bitwise operators in general.

    Cheers - L~R