Unbias a random generator: Difference between revisions

Content added Content deleted
(→‎{{header|CoffeeScript}}: Range of N is wrong)
(use proper range)
Line 221: Line 221:


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
{{incomplete|CoffeeScript|range of N is 3..6 not 5..9}}
<lang coffeescript>
<lang coffeescript>
biased_rand_function = (n) ->
biased_rand_function = (n) ->
Line 246: Line 245:
console.log "ratio of 1s: #{cnt / sample_size} [#{label}]"
console.log "ratio of 1s: #{cnt / sample_size} [#{label}]"
for n in [5..9]
for n in [3..6]
console.log "\n---------- n = #{n}"
console.log "\n---------- n = #{n}"
f_biased = biased_rand_function(n)
f_biased = biased_rand_function(n)
Line 256: Line 255:
<lang>
<lang>
> coffee unbiased.coffee
> coffee unbiased.coffee

---------- n = 3
ratio of 1s: 0.3333343 [biased]
ratio of 1s: 0.4999514 [unbiased]

---------- n = 4
ratio of 1s: 0.2499751 [biased]
ratio of 1s: 0.4998067 [unbiased]


---------- n = 5
---------- n = 5
ratio of 1s: 0.1999817 [biased]
ratio of 1s: 0.199729 [biased]
ratio of 1s: 0.4998929 [unbiased]
ratio of 1s: 0.5003183 [unbiased]


---------- n = 6
---------- n = 6
ratio of 1s: 0.1666272 [biased]
ratio of 1s: 0.1664843 [biased]
ratio of 1s: 0.4999431 [unbiased]
ratio of 1s: 0.4997813 [unbiased]

---------- n = 7
ratio of 1s: 0.1429804 [biased]
ratio of 1s: 0.5000402 [unbiased]

---------- n = 8
ratio of 1s: 0.1249847 [biased]
ratio of 1s: 0.500279 [unbiased]

---------- n = 9
ratio of 1s: 0.1110524 [biased]
ratio of 1s: 0.4999337 [unbiased]
</lang>
</lang>