How to resolve the algorithm Unbias a random generator step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Unbias a random generator step by step in the FreeBASIC 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 FreeBASIC programming language
Source code in the freebasic programming language
Function randN (n As Ubyte) As Ubyte
If Int(Rnd * n) + 1 <> 1 Then Return 0 Else Return 1
End Function
Function unbiased (n As Ubyte) As Ubyte
Dim As Ubyte a, b
Do
a = randN (n)
b = randN (n)
Loop Until a <> b
Return a
End Function
Const count = 100000
Dim x As Ubyte
Randomize Timer
Print "Resultados de n";Chr(163);!"meros aleatorios sesgados e imparciales\n"
For n As Ubyte = 3 To 6
Dim As Integer b_count(1)
Dim As Integer u_count(1)
For m As Integer = 1 To count
x = randN (n)
b_count(x) += 1
x = unbiased (n)
u_count(x) += 1
Next m
Print "N ="; n
Print " Biased =>", "#0="; Str(b_count(0)), "#1="; Str(b_count(1)),
Print Using "ratio = ##.##%"; (b_count(1) / count * 100)
Print "Unbiased =>", "#0="; Str(u_count(0)), "#1="; Str(u_count(1)),
Print Using "ratio = ##.##%"; (u_count(1) / count * 100)
Next n
Sleep
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Zig-zag matrix step by step in the Delphi programming language
You may also check:How to resolve the algorithm Vampire number step by step in the Raku programming language