Generic Type Method


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


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



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

Return Value

Either true or false, randomly chosen with equal probability.


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

static func random() -> Bool

Returns a random Boolean value.