Unbias a random generator: Difference between revisions
Content added Content deleted
(Go solution) |
(→{{header|Euphoria}}: Euphoria example added) |
||
Line 156: | Line 156: | ||
5: 19.61% 50.01% |
5: 19.61% 50.01% |
||
6: 16.70% 49.98%</pre> |
6: 16.70% 49.98%</pre> |
||
=={{header|Euphoria}}== |
|||
<lang euphoria>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> |
|||
Output: |
|||
<pre>3: 33.68% 49.94% |
|||
4: 24.93% 50.48% |
|||
5: 20.32% 49.97% |
|||
6: 16.98% 50.05% |
|||
</pre> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |