Difference between revisions of "Code Snippet: Random Boolean"

From Coder Merlin
Line 8: Line 8:
     print("Tails")
     print("Tails")
}
}
</syntaxhighlight>
<syntaxhighlight lang="swift">
// Simulate flipping a coin and print the result (with syntactic sugar)
let headsOrTails = Bool.random() ? "Heads" : "Tails"
print(headsOrTails)
</syntaxhighlight>
</syntaxhighlight>

Revision as of 00:03, 4 December 2018

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

Generate a Random Boolean[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)