How to resolve the algorithm Unbias a random generator step by step in the ERRE programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Unbias a random generator step by step in the ERRE 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 ERRE programming language
Source code in the erre programming language
PROGRAM UNBIAS
FUNCTION RANDN(N)
RANDN=INT(1+N*RND(1))=1
END FUNCTION
PROCEDURE UNBIASED(N->RIS)
LOCAL A,B
REPEAT
A=RANDN(N)
B=RANDN(N)
UNTIL A<>B
RIS=A
END PROCEDURE
BEGIN
PRINT(CHR$(12);) ! CLS
RANDOMIZE(TIMER)
FOR N=3 TO 6 DO
BIASED=0
UNBIASED=0
FOR I=1 TO 10000 DO
IF RANDN(N) THEN biased+=1
UNBIASED(N->RIS)
IF RIS THEN unbiased+=+1
END FOR
PRINT("N =";N;" : biased =";biased/100;", unbiased =";unbiased/100)
END FOR
END PROGRAM
You may also check:How to resolve the algorithm Multiple distinct objects step by step in the Factor programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the BASIC programming language
You may also check:How to resolve the algorithm Bell numbers step by step in the Elixir programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Maple programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Factor programming language