Difference between revisions of "Code Snippet: Random Boolean"
From Coder Merlin
Nerdofcode (talk | contribs) m (Updated Python snippet to match other snippet's layout) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
= Generate a Random Boolean = | |||
== Swift == | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
// Simulate flipping a coin and print the result | // Simulate flipping a coin and print the result | ||
Line 13: | Line 15: | ||
// Simulate flipping a coin and print the result (with syntactic sugar) | // Simulate flipping a coin and print the result (with syntactic sugar) | ||
let headsOrTails = Bool.random() ? "Heads" : "Tails" | let headsOrTails = Bool.random() ? "Heads" : "Tails" | ||
print(headsOrTails) | |||
</syntaxhighlight> | |||
== Python == | |||
<syntaxhighlight lang="python"> | |||
import random | |||
headsOrTails = random.choice([True, False]) | |||
print(headsOrTails) | print(headsOrTails) | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 13:51, 13 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]
import random
headsOrTails = random.choice([True, False])
print(headsOrTails)