Fastest way to generate a random boolean

The first optionrand.Next(2) executes behind the scenes the following code:

if (maxValue < 0)
{
    throw new ArgumentOutOfRangeException("maxValue",
        Environment.GetResourceString("ArgumentOutOfRange_MustBePositive", new object[] { "maxValue" }));
}
return (int) (this.Sample() * maxValue);

and for the second optionrand.NextDouble():

return this.Sample();

Since the first option contains maxValue validation, multiplication and casting, the second option is probably faster.

Leave a Comment