Code Snippet: Random Integers
From Coder Merlin
Revision as of 00:02, 4 December 2018 by Chukwuemeka-tinashe (talk | contribs) (→Generate a Random Integer)
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)