Code Snippet: Random Integers

From Coder Merlin
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)