How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Haskell programming language
How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Haskell 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 Haskell programming language
The code you provided generates a random 7-sided die roll using 5-sided dice, and checks if the distribution of the results is uniform.
The function sevenFrom5Dice
generates a random 7-sided die roll by rolling two 5-sided dice and then multiplying the first roll by 5, adding the second roll, and subtracting 6. If the result is greater than 20, the function is called recursively. Otherwise, the result is returned modulo 7.
The function replicateM
replicates an action a specified number of times and collects the results in a list. In the expression replicateM 10 sevenFrom5Dice
, the function sevenFrom5Dice
is called 10 times and the results are collected in a list.
The function mapM_
applies a function to each element of a list. In the expression mapM_ print .sort =<< distribCheck sevenFrom5Dice 1000000 3
, the function print
is applied to each element of the list returned by the function distribCheck
, the results are sorted, and then the sorted list is printed.
The function distribCheck
generates a list of n
random 7-sided die rolls and checks if the distribution of the results is uniform. The first argument to distribCheck
is the function that generates the random die rolls, the second argument is the number of die rolls to generate, and the third argument is the number of bins to use when checking the distribution.
The function distribCheck
first generates a list of n
random die rolls using the provided function. Then, the function creates a list of n
bins, each of which is initialized to 0. For each die roll, the function finds the bin corresponding to the die roll and increments the count in that bin.
Finally, the function checks if the distribution of the die rolls is uniform by comparing the counts in the bins to the expected count. The expected count is n/b
, where n
is the number of die rolls and b
is the number of bins.
The output of the program shows that the distribution of the die rolls is uniform.
Source code in the haskell programming language
import System.Random
import Data.List
sevenFrom5Dice = do
d51 <- randomRIO(1,5) :: IO Int
d52 <- randomRIO(1,5) :: IO Int
let d7 = 5*d51+d52-6
if d7 > 20 then sevenFrom5Dice
else return $ 1 + d7 `mod` 7
*Main> replicateM 10 sevenFrom5Dice
[2,3,1,1,6,2,5,6,5,3]
*Main> mapM_ print .sort =<< distribCheck sevenFrom5Dice 1000000 3
(1,(142759,True))
(2,(143078,True))
(3,(142706,True))
(4,(142403,True))
(5,(142896,True))
(6,(143028,True))
(7,(143130,True))
You may also check:How to resolve the algorithm Convex hull step by step in the Groovy programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the C++ programming language
You may also check:How to resolve the algorithm Parse an IP Address step by step in the Go programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the D programming language
You may also check:How to resolve the algorithm Cuban primes step by step in the J programming language