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 [ |
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 = 5 |
---------- n = 5 |
||
ratio of 1s: 0. |
ratio of 1s: 0.199729 [biased] |
||
ratio of 1s: 0. |
ratio of 1s: 0.5003183 [unbiased] |
||
---------- n = 6 |
---------- n = 6 |
||
ratio of 1s: 0. |
ratio of 1s: 0.1664843 [biased] |
||
ratio of 1s: 0. |
ratio of 1s: 0.4997813 [unbiased] |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
---------- n = 9 |
|||
ratio of 1s: 0.1110524 [biased] |
|||
ratio of 1s: 0.4999337 [unbiased] |
|||
</lang> |
</lang> |
||