How to resolve the algorithm Unbias a random generator step by step in the Clojure programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Unbias a random generator step by step in the Clojure programming language

Table of Contents

Problem Statement

The actual unbiasing should be done by generating two numbers at a time from randN and only returning a 1 or 0 if they are different. As long as you always return the first number or always return the second number, the probabilities discussed above should take over the biased probability of randN. This task is an implementation of Von Neumann debiasing, first described in a 1951 paper.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Unbias a random generator step by step in the Clojure programming language

Source code in the clojure programming language

(defn biased [n]
  (if (< (rand 2) (/ n)) 0 1))

(defn unbiased [n]
  (loop [a 0 b 0]
    (if (= a b)
      (recur (biased n) (biased n))
      a)))

(for [n (range 3 7)]
  [n
   (double (/ (apply + (take 50000 (repeatedly #(biased n)))) 50000))
   (double (/ (apply + (take 50000 (repeatedly #(unbiased n)))) 50000))])
([3 0.83292 0.50422]
 [4 0.87684 0.5023]
 [5 0.90122 0.49728]
 [6 0.91526 0.5])


  

You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Sparkling programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Plain TeX programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the VBA programming language
You may also check:How to resolve the algorithm Range consolidation step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Tic-tac-toe step by step in the Phix programming language