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)
 
(8 intermediate revisions by 4 users not shown)
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">
<syntaxhighlight lang="python">
// Generate a fraction and print the result
from random import randint
let fraction = Float.random(in: 0 ..< 1)
print(randint(1,6))
print(fraction)
</syntaxhighlight>
 
<syntaxhighlight lang="python">
from random import randint
print(randint(1,6) + randint(1,6))
</syntaxhighlight>
 
== Common Lisp ==
<syntaxhighlight lang="lisp">
; Seed the random state
(setf *random-state* (make-random-state t))
 
(defvar firstDie)
(setq firstDie (random 6))
 
(format t "~d~%"
firstDie)
</syntaxhighlight>
 
<syntaxhighlight lang="lisp">
; Seed the random state
(setf *random-state* (make-random-state t))
 
(defvar firstDie)
(defvar secondDie)
(defvar dieResults)
 
(setq firstDie (random 6))
(setq secondDie (random 6))
(setq dieResults (+ firstDie secondDie))
 
(format t "~d~%"
dieResults)
</syntaxhighlight>
 
== Java ==
<syntaxhighlight lang="java">
// Import the Random class in java.util
Random random = new Random();
 
// Simulate rolling a 6 sided dice
// nextInt returns a pseudo random integer between 0(inclusive) and upperBound(exclusive)
int roll1 = random.nextInt(6) + 1; // Added the 1 to shift from 0 to 5 to 1 to 6
System.out.println("You rolled a " + roll1 + "!");
 
// Add a second die
int roll2 = random.nextInt(6) + 1;
System.out.println("You rolled a " + roll2 + "!");
int sum = roll1 + roll2;
System.out.println("Your sum is " + sum);
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 09:24, 19 September 2019

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]

from random import randint
print(randint(1,6))
from random import randint
print(randint(1,6) + randint(1,6))

Common Lisp[edit]

; Seed the random state
(setf *random-state* (make-random-state t))

(defvar firstDie)
(setq firstDie (random 6))

(format t "~d~%"
	firstDie)
; Seed the random state
(setf *random-state* (make-random-state t))

(defvar firstDie)
(defvar secondDie)
(defvar dieResults)

(setq firstDie (random 6))
(setq secondDie (random 6))
(setq dieResults (+ firstDie secondDie))

(format t "~d~%"
	dieResults)

Java[edit]

// Import the Random class in java.util
Random random = new Random();

// Simulate rolling a 6 sided dice
// nextInt returns a pseudo random integer between 0(inclusive) and upperBound(exclusive)
int roll1 = random.nextInt(6) + 1; // Added the 1 to shift from 0 to 5 to 1 to 6
System.out.println("You rolled a " + roll1 + "!");

// Add a second die
int roll2 = random.nextInt(6) + 1;
System.out.println("You rolled a " + roll2 + "!");
int sum = roll1 + roll2;
System.out.println("Your sum is " + sum);