Difference between revisions of "Code Snippet: Random Integers"
From Coder Merlin
(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...") |
|||
Line 13: | Line 13: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Generate a Random Fraction == | |||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
// Generate a fraction and print the result | // Generate a fraction and print the result |
Revision as of 00:02, 4 December 2018
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 Random Fraction[edit]
// Generate a fraction and print the result
let fraction = Float.random(in: 0 ..< 1)
print(fraction)