Difference between revisions of "Code Snippet: Random Integers"

From Coder Merlin
m (Merlin moved page Code Snippet: Random Numbers to Code Snippet: Random Integers without leaving a redirect)
Line 1: Line 1:
== Generate a Random Integer ==
= Generate a Random Integer =
 
== Swift ==
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
// Simulate casting a single, six-sided die and print the result
// Simulate casting a single, six-sided die and print the result
Line 13: Line 15:
</syntaxhighlight>
</syntaxhighlight>


== Generate a Random Fraction ==
== Python ==
<syntaxhighlight lang="swift">
// Generate a fraction and print the result
let fraction = Float.random(in: 0 ..< 1)
print(fraction)
</syntaxhighlight>

Revision as of 17:40, 24 December 2018

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Generate a Random Integer[edit]

Swift[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)

Python[edit]