Jump to content

Unbias a random generator: Difference between revisions

adding gap
(→‎{{header|Perl 6}}: Added Perl 6 solution)
(adding gap)
Line 115:
5: 19.971% 49.987%
6: 16.688% 50.097%</pre>
 
=={{header|GAP}}==
<lang gap>RandNGen := function(n)
local v, rand;
v := [1 .. n - 1]*0;
Add(v, 1);
rand := function()
return Random(v);
end;
return rand;
end;
 
UnbiasedGen := function(rand)
local unbiased;
unbiased := function()
local a, b;
while true do
a := rand();
b := rand();
if a <> b then
break;
fi;
od;
return a;
end;
return unbiased;
end;
 
a := RandNGen(2);
b := RandNGen(5);
c := UnbiasedGen(a);
d := UnbiasedGen(b);
 
Sum([1 .. 1000000], n -> a());
# 500105
 
Sum([1 .. 1000000], n -> b());
# 200512
 
Sum([1 .. 1000000], n -> c());
# 500158
 
Sum([1 .. 1000000], n -> d());
# 499752</lang>
 
=={{header|J}}==
506

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.