How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Clojure 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 Clojure 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 Clojure programming language
Source code in the clojure programming language
(def dice5 #(rand-int 5))
(defn dice7 []
(quot (->> dice5 ; do the following to dice5
(repeatedly 2) ; call it twice
(apply #(+ %1 (* 5 %2))) ; d1 + 5*d2 => 0..24
#() ; wrap that up in a function
repeatedly ; make infinite sequence of the above
(drop-while #(> % 20)) ; throw away anything > 20
first) ; grab first acceptable element
3)) ; divide by three rounding down
(doseq [n [100 1000 10000] [num count okay?] (verify dice7 n)]
(println "Saw" num count "times:"
(if okay? "that's" " not") "acceptable"))
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the Mathprog programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the Ruby programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the F# programming language
You may also check:How to resolve the algorithm Solve a Holy Knight's tour step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Aikido programming language