Difference between revisions of "Code Snippet: Random Boolean"

From Coder Merlin
(Added Python Random Bool)
Line 19: Line 19:


== Python ==
== Python ==
<syntaxhighlight lang="python">
random.choice([True, False])
</syntaxhighlight>

Revision as of 15:14, 11 September 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Generate a Random Boolean[edit]

Swift[edit]

// 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[edit]

random.choice([True, False])