Matti Nykyri <matti.nyk...@iki.fi> schrieb:

> If you are looking a mathematically perfect solution there is a simple
> one even if your list is not in the power of 2! Take 6 bits at a time of
> the random data. If the result is 62 or 63 you will discard the data and
> get the next 6 bits. This selectively modifies the random data but keeps
> the probabilities in correct balance. Now the probability for index of
> 0-61 is 1/62 because the probability to get 62-63 out of 64 if 0.

Why not do just something like this?

index = 0;
while (true) {
  index = (index + get_6bit_random()) % 62;
  output << char_array[index];
}

Done, no bits wasted. Should have perfect distribution also. We also don't 
have to throw away random data just to stay within unaligned boundaries. The 
unalignment is being taken over into the next loop so the "error" corrects 
itself over time (it becomes distributed over the whole set).

-- 
Replies to list only preferred.


Reply via email to