Shuffle
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Fri Jan 25 15:37:03 PST 2008
Walter Bright wrote:
> Frits van Bommel wrote:
[snip stuff I was right about :)]
>
>> To fix that, ignore the result of rand() if it's > ((RAND_MAX /
>> files.length) * files.length), for example by replacing the first line
>> of the loop with:
>> ---
>> uint r = std.random.max;
>> // assumes RAND_MAX == uint.max
>> auto max = (uint.max / files.length) * files.length;
>> while (r > max)
>> r = std.random.rand();
>> auto j = r % files.length;
>> ---
>> (also, add "assert(files.length <= RAND_MAX);" before the loop if
>> RAND_MAX < uint.max)
>
> I think it's (r >= max).
You're right, that loop condition was missing an '='. I was typing all
that from memory of a class I took years ago, so I'm just glad I got the
most important stuff right.
More information about the Digitalmars-d-announce
mailing list