Jump to content

Unbias a random generator: Difference between revisions

Added BBC BASIC
(→‎{{header|Mathematica}}: Marked incorrect. The task asks for specific output to be shown that is missing.)
(Added BBC BASIC)
Line 85:
StringReplace, junk, t, 1, , UseErrorLevel
MsgBox % "biased probability of a 1 occurring: " Errorlevel/1000</lang>
 
=={{header|BBC BASIC}}==
<lang bbcbasic> FOR N% = 3 TO 6
biased% = 0
unbiased% = 0
FOR I% = 1 TO 10000
IF FNrandN(N%) biased% += 1
IF FNunbiased(N%) unbiased% += 1
NEXT
PRINT "N = ";N% " : biased = "; biased%/100 "%, unbiased = "; unbiased%/100 "%"
NEXT
END
DEF FNunbiased(N%)
LOCAL A%,B%
REPEAT
A% = FNrandN(N%)
B% = FNrandN(N%)
UNTIL A%<>B%
= A%
DEF FNrandN(N%) = -(RND(N%) = 1)</lang>
Output:
<pre>
N = 3 : biased = 33.57%, unbiased = 49.94%
N = 4 : biased = 25.34%, unbiased = 50.76%
N = 5 : biased = 20.06%, unbiased = 50.04%
N = 6 : biased = 16.25%, unbiased = 50.13%
</pre>
 
=={{header|C}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.