Code Snippet: Random Integers

From Coder Merlin
Revision as of 23:56, 3 December 2018 by Chukwuemeka-tinashe (talk | contribs) (Created page with "== Generate a Random Integer == <syntaxhighlight lang="swift"> // Simulate casting a single, six-sided die and print the result let firstDie = Int.random(in:1...6) print(first...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Generate a Random Integer[edit]

// Simulate casting a single, six-sided die and print the result
let firstDie = Int.random(in:1...6)
print(firstDie)
// Simulate casting two six-sided dice and print the sum
let firstDie = Int.random(in:1...6)
let secondDie = Int.random(in:1...6)
print(firstDie+secondDie)
// Generate a fraction and print the result
let fraction = Float.random(in: 0 ..< 1)
print(fraction)