Type Method

random()

Returns a random Boolean value.

Declaration

static func random() -> Bool

Return Value

Either true or false, randomly chosen with equal probability.

Discussion

This method returns true and false with equal probability.

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

This method is equivalent to calling Bool.random(using:), passing in the system’s default random generator.

See Also

Creating a Random Value

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

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