How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Scala programming language
Table of Contents
Problem Statement
(Given an equal-probability generator of one of the integers 1 to 5 as dice5), create dice7 that generates a pseudo-random integer from 1 to 7 in equal probability using only dice5 as a source of random numbers, and check the distribution for at least one million calls using the function created in Simple Random Distribution Checker.
Implementation suggestion: dice7 might call dice5 twice, re-call if four of the 25 combinations are given, otherwise split the other 21 combinations into 7 groups of three, and return the group index from the rolls. (Task adapted from an answer here)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Scala programming language
Source code in the scala programming language
import scala.util.Random
object SevenSidedDice extends App {
private val rnd = new Random
private def seven = {
var v = 21
def five = 1 + rnd.nextInt(5)
while (v > 20) v = five + five * 5 - 6
1 + v % 7
}
println("Random number from 1 to 7: " + seven)
}
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the 11l programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Integer BASIC programming language
You may also check:How to resolve the algorithm Comments step by step in the E programming language