June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,45 @@
|
|||
import extensions.
|
||||
|
||||
int extension $op
|
||||
{
|
||||
bool randN
|
||||
= randomGenerator nextInt(self) == 0.
|
||||
|
||||
bool unbiased
|
||||
[
|
||||
bool flip1 := self randN.
|
||||
bool flip2 := self randN.
|
||||
|
||||
while (flip1 == flip2)
|
||||
[
|
||||
flip1 := self randN.
|
||||
flip2 := self randN.
|
||||
].
|
||||
|
||||
^ flip1
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
3 to:6 do(:n)<int>
|
||||
[
|
||||
int biasedZero := 0.
|
||||
int biasedOne := 0.
|
||||
int unbiasedZero := 0.
|
||||
int unbiasedOne := 0.
|
||||
|
||||
0 till:100000 do(:i)
|
||||
[
|
||||
if (n randN) [ biasedOne += 1 ]; [ biasedZero += 1 ].
|
||||
if (n unbiased) [ unbiasedOne += 1 ]; [ unbiasedZero += 1 ]
|
||||
].
|
||||
|
||||
console
|
||||
printLineFormatted("(N = {0}):" padRight(17) + "# of 0"$9"# of 1"$9"% of 0"$9"% of 1", n);
|
||||
printLineFormatted("Biased:" padRight(15) + "{0}"$9"{1}"$9"{2}"$9"{3}",
|
||||
biasedZero, biasedOne, biasedZero / 1000, biasedOne / 1000);
|
||||
printLineFormatted("Unbiased:" padRight(15) + "{0}"$9"{1}"$9"{2}"$9"{3}",
|
||||
unbiasedZero, unbiasedOne, unbiasedZero / 1000, unbiasedOne / 1000).
|
||||
]
|
||||
].
|
||||
Loading…
Add table
Add a link
Reference in a new issue