Unbias a random generator: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,155: | Line 1,155: | ||
N=5 randN: [792, 208], 20.8% unbiased: [494, 506], 50.6% |
N=5 randN: [792, 208], 20.8% unbiased: [494, 506], 50.6% |
||
N=6 randN: [834, 166], 16.6% unbiased: [514, 486], 48.6%</pre> |
N=6 randN: [834, 166], 16.6% unbiased: [514, 486], 48.6%</pre> |
||
=={{header|Phix}}== |
|||
Copy of [[Unbias_a_random_generator#Euphoria|Euphoria]] |
|||
<lang Phix>function randN(integer N) |
|||
return rand(N) = 1 |
|||
end function |
|||
function unbiased(integer N) |
|||
integer a |
|||
while 1 do |
|||
a = randN(N) |
|||
if a!=randN(N) then |
|||
return a |
|||
end if |
|||
end while |
|||
end function |
|||
constant n = 10000 |
|||
integer cb, cu |
|||
for b=3 to 6 do |
|||
cb = 0 |
|||
cu = 0 |
|||
for i=1 to n do |
|||
cb += randN(b) |
|||
cu += unbiased(b) |
|||
end for |
|||
printf(1, "%d: %5.2f%% %5.2f%%\n", {b, 100 * cb / n, 100 * cu / n}) |
|||
end for</lang> |
|||
{{out}} |
|||
<pre> |
|||
3: 32.83% 50.34% |
|||
4: 24.78% 50.01% |
|||
5: 20.21% 49.71% |
|||
6: 16.68% 49.67% |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |