Contents

random(using:)

Returns a random Boolean value, using the given generator as a source for randomness.

Declaration

static func random<T>(using generator: inout T) -> Bool where T : RandomNumberGenerator

Parameters

  • generator:

    The random number generator to use when creating the new random value.

Return Value

Either true or false, randomly chosen with equal probability.

Discussion

This method returns true and false with equal probability. Use this method to generate a random Boolean value when you are using a custom random number generator.

let flippedHeads = Bool.random(using: &myGenerator)
if flippedHeads {
    print("Heads, you win!")
} else {
    print("Maybe another try?")
}

See Also

Creating a Random Value