Unbias a random generator: Difference between revisions

Added R.
m (→‎{{header|Ruby}}: removed a space)
(Added R.)
Line 1,201:
Biased(6) = Stats(count1=167561, count0=832439, percent=16.7561)
Unbiased = Stats(count1=499963, count0=500037, percent=49.996299999999998)</pre>
 
=={{header|R}}==
 
<lang rsplus>randN = function(N) sample.int(N, 1) == 1
 
unbiased = function(f)
{while ((x <- f()) == f()) {}
x}
 
samples = 10000
print(t(round(d = 2, sapply(3:6, function(N) c(
N = N,
biased = mean(replicate(samples, randN(N))),
unbiased = mean(replicate(samples, unbiased(function() randN(N)))))))))</lang>
 
Sample output:
 
<pre> N biased unbiased
[1,] 3 0.32 0.50
[2,] 4 0.24 0.50
[3,] 5 0.21 0.49
[4,] 6 0.16 0.51</pre>
 
=={{header|Racket}}==
845

edits