June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,7 +1,7 @@
integer
biased(integer bias)
{
return 1 ^ min(drand(bias - 1), 1);
1 ^ min(drand(bias - 1), 1);
}
integer
@ -12,7 +12,7 @@ unbiased(integer bias)
while ((a = biased(bias)) == biased(bias)) {
}
return a;
a;
}
integer
@ -23,21 +23,17 @@ main(void)
n = 10000;
b = 3;
while (b <= 6) {
i = 0;
cb = 0;
cu = 0;
while (i < n) {
i = cb = cu = 0;
while ((i += 1) <= n) {
cb += biased(b);
cu += unbiased(b);
i += 1;
}
o_form("bias ~: /d2p2/%% vs /d2p2/%%\n", b, 100r * cb / n,
100r * cu / n);
100r * cu / n);
b += 1;
}
return 0;
0;
}

View file

@ -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).
]
].

View file

@ -0,0 +1,28 @@
// version 1.1.2
fun biased(n: Int) = Math.random() < 1.0 / n
fun unbiased(n: Int): Boolean {
var a: Boolean
var b: Boolean
do {
a = biased(n)
b = biased(n)
}
while (a == b)
return a
}
fun main(args: Array<String>) {
val m = 50_000
val f = "%d: %2.2f%% %2.2f%%"
for (n in 3..6) {
var c1 = 0
var c2 = 0
for (i in 0 until m) {
if (biased(n)) c1++
if (unbiased(n)) c2++
}
println(f.format(n, 100.0 * c1 / m, 100.0 * c2 / m))
}
}

View file

@ -0,0 +1,20 @@
randN(N) = () -> rand(1:N) == 1 ? 1 : 0
function unbiased(biased::Function)
this, that = biased(), biased()
while this == that this, that = biased(), biased() end
return this
end
@printf "%2s | %10s | %5s | %5s | %8s" "N" "bias./unb." "1s" "0s" "pct ratio"
const nrep = 10000
for N in 3:6
biased = randN(N)
v = collect(biased() for __ in 1:nrep)
v1, v0 = count(v .== 1), count(v .== 0)
@printf("%2i | %10s | %5i | %5i | %5.2f%%\n", N, "biased", v1, v0, 100 * v1 / nrep)
v = collect(unbiased(biased) for __ in 1:nrep)
v1, v0 = count(v .== 1), count(v .== 0)
@printf("%2i | %10s | %5i | %5i | %5.2f%%\n", N, "unbiased", v1, v0, 100 * v1 / nrep)
end

View file

@ -4,7 +4,7 @@ if #=='' | #=="," then #=1000 /*#: the number of SAMPLES to
if R=='' | R=="," then R=6 /*R: the high number for the range. */
if datatype(seed, 'W') then call random ,,seed /*Specified? Then use for RANDOM seed.*/
dash=''; @b="biased"; @ub='un'@b /*literals for the SAY column headers. */
say left('',5) ctr('N',5) ctr(@b) ctr(@b"%") ctr(@ub) ctr(@ub'%') ctr("samples")
say left('',5) ctr("N",5) ctr(@b) ctr(@b'%') ctr(@ub) ctr(@ub"%") ctr('samples')
dash=
do N=3 to R; b=0; u=0
do j=1 for #; b=b + randN(N); u=u + unbiased()

View file

@ -0,0 +1,39 @@
#![feature(inclusive_range_syntax)]
extern crate rand;
use rand::Rng;
fn rand_n<R: Rng>(rng: &mut R, n: u32) -> usize {
rng.gen_weighted_bool(n) as usize // maps `false` to 0 and `true` to 1
}
fn unbiased<R: Rng>(rng: &mut R, n: u32) -> usize {
let mut bit = rand_n(rng, n);
while bit == rand_n(rng, n) {
bit = rand_n(rng, n);
}
bit
}
fn main() {
const SAMPLES: usize = 100_000;
let mut rng = rand::weak_rng();
println!(" Bias rand_n unbiased");
for n in 3..=6 {
let mut count_biased = 0;
let mut count_unbiased = 0;
for _ in 0..SAMPLES {
count_biased += rand_n(&mut rng, n);
count_unbiased += unbiased(&mut rng, n);
}
let b_percentage = 100.0 * count_biased as f64 / SAMPLES as f64;
let ub_percentage = 100.0 * count_unbiased as f64 / SAMPLES as f64;
println!(
"bias {}: {:0.2}% {:0.2}%",
n, b_percentage, ub_percentage
);
}
}