Unbias a random generator: Difference between revisions

(Added BBC BASIC)
Line 820:
 
=={{header|Mathematica}}==
<lang Mathematica>randNrand[bias_, n_] := If[Mod[#, 13] == 0, 1, 0]- & /Unitize@ RandomInteger[13bias - 1, n]
{{incorrect|Mathematica|The task asks for specific output to be shown that is missing.}}
<lang Mathematica>randN[n_] := If[Mod[#, 13] == 0, 1, 0] & /@ RandomInteger[13, n]
 
unbiased[bias_, n_] := Module[{b = {}, a = 0},
DeleteCases[rand[bias, {n, 2}], {a_, a_}][[All, 1]]</lang>
While[Length[b] < n, a = randN[2];
If[Differences[a] != {0}, AppendTo[b, First[a]]]]; b
]</lang>
 
<pre>randN[10]count = 1000000;
TableForm[
-> {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}
Table[{n, Total[rand[n, count]]/count // N,
Total[#]/Length[#] &@unbiased[n, count] // N}, {n, 3, 6}],
TableHeadings -> {None, {n, "biased", "unbiased"}}]
 
n biased unbiased[10]
3 0.33312 0.500074
->{0, 1, 0, 0, 0, 0, 1, 1, 1, 0}</pre>
4 0.24932 0.499883
5 0.1998 0.498421
6 0.16620 0.49805
 
</pre>
 
=={{header|OCaml}}==
Anonymous user