Unbias a random generator: Difference between revisions
Content added Content deleted
(Updated to work with Nim 1.4: replaced "math" with "random", removed "newSeqWith", added missing parameter types, replaced "random" with "rand". Also changed output using "strformat".) |
(Added Quackery.) |
||
Line 1,641: | Line 1,641: | ||
[3,] 5 0.21 0.49 |
[3,] 5 0.21 0.49 |
||
[4,] 6 0.16 0.51</pre> |
[4,] 6 0.16 0.51</pre> |
||
=={{header|Quackery}}== |
|||
<lang Quackery> $ "bigrat.qky" loadfile |
|||
[ random 0 = ] is randN ( n --> n ) |
|||
[ dup randN |
|||
over randN |
|||
2dup = iff |
|||
2drop again |
|||
drop nip ] is unbias ( n --> n ) |
|||
[ dup echo say " biased --> " |
|||
0 |
|||
1000000 times |
|||
[ over randN if 1+ ] |
|||
nip 1000000 6 point$ echo$ ] is showbias ( n --> ) |
|||
[ dup echo say " unbiased --> " |
|||
0 |
|||
1000000 times |
|||
[ over unbias if 1+ ] |
|||
nip 1000000 6 point$ echo$ ] is showunbias ( n --> ) |
|||
' [ 3 4 5 6 ] |
|||
witheach |
|||
[ dup cr |
|||
showbias cr |
|||
showunbias cr ] </lang> |
|||
{{out}} |
|||
<pre>3 biased --> 0.333225 |
|||
3 unbiased --> 0.500147 |
|||
4 biased --> 0.249658 |
|||
4 unbiased --> 0.499851 |
|||
5 biased --> 0.200169 |
|||
5 unbiased --> 0.500073 |
|||
6 biased --> 0.166804 |
|||
6 unbiased --> 0.499045 |
|||
</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |