How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Racket 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 Racket 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 Racket programming language

Source code in the racket programming language

#lang racket
(define (dice5) (add1 (random 5)))

(define (dice7)
  (define res (+ (* 5 (dice5)) (dice5) -6))
  (if (< res 21) (+ 1 (modulo res 7)) (dice7)))


-> (require math/statistics)
-> (samples->hash (for/list ([i 700000]) (dice7)))
'#hash((7 . 100392)
       (6 . 100285)
       (5 . 99774)
       (4 . 100000)
       (3 . 100000)
       (2 . 99927)
       (1 . 99622))


  

You may also check:How to resolve the algorithm Square-free integers step by step in the Julia programming language
You may also check:How to resolve the algorithm Polynomial long division step by step in the Python programming language
You may also check:How to resolve the algorithm Generic swap step by step in the REBOL programming language
You may also check:How to resolve the algorithm SEDOLs step by step in the zkl programming language
You may also check:How to resolve the algorithm Break OO privacy step by step in the OCaml programming language