How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Hy programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Hy programming language
Table of Contents
Problem Statement
This task is an adjunct to Seven-sided dice from five-sided dice.
Create a function to check that the random integers returned from a small-integer generator function have uniform distribution.
The function should take as arguments:
The function should produce:
Show the distribution checker working when the produced distribution is flat enough and when it is not. (Use a generator from Seven-sided dice from five-sided dice).
See also:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Verify distribution uniformity/Naive step by step in the Hy programming language
Source code in the hy programming language
(import [collections [Counter]])
(import [random [randint]])
(defn uniform? [f repeats delta]
; Call 'f' 'repeats' times, then check if the proportion of each
; value seen is within 'delta' of the reciprocal of the count
; of distinct values.
(setv bins (Counter (list-comp (f) [i (range repeats)])))
(setv target (/ 1 (len bins)))
(all (list-comp
(<= (- target delta) (/ n repeats) (+ target delta))
[n (.values bins)])))
(for [f [
(fn [] (randint 1 10))
(fn [] (if (randint 0 1) (randint 1 9) (randint 1 10)))]]
(print (uniform? f 5000 .02)))
You may also check:How to resolve the algorithm Population count step by step in the Wren programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sleep step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Integer overflow step by step in the Scala programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the Ring programming language