Code Snippet: Random Boolean

From Coder Merlin™

Generate a Random Boolean

Swift

// Simulate flipping a coin and print the result
let isHeads = Bool.random()
if isHeads {
    print("Heads")
} else {
    print("Tails")
}
// Simulate flipping a coin and print the result (with syntactic sugar)
let headsOrTails = Bool.random() ? "Heads" : "Tails"
print(headsOrTails)

Python

import random

headsOrTails = random.choice([True, False])
print(headsOrTails)