September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
@ -3,20 +3,18 @@ randomize()
|
|||
|
||||
template newSeqWith(len: int, init: expr): expr =
|
||||
var result {.gensym.} = newSeq[type(init)](len)
|
||||
for i in 0 .. <len:
|
||||
for i in 0..<len:
|
||||
result[i] = init
|
||||
result
|
||||
|
||||
proc randN(n): (proc: range[0..1]) =
|
||||
result = proc(): range[0..1] =
|
||||
if random(n) == 0: 1 else: 0
|
||||
proc: range[0..1] = ord(random(n) == 0)
|
||||
|
||||
proc unbiased(biased): range[0..1] =
|
||||
var (this, that) = (biased(), biased())
|
||||
while this == that:
|
||||
this = biased()
|
||||
result = biased()
|
||||
var that = biased()
|
||||
while result == that:
|
||||
result = biased()
|
||||
that = biased()
|
||||
return this
|
||||
|
||||
for n in 3..6:
|
||||
var biased = randN(n)
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
/*REXX program generates unbiased random numbers and displays the results to terminal.*/
|
||||
parse arg # R seed . /*get optional parameters from the CL. */
|
||||
if #=='' | #=="," then #=1000 /*# the number of SAMPLES to be used.*/
|
||||
if R=='' | R=="," then R=6 /*R the high number for the range. */
|
||||
if datatype(seed, 'W') then call random ,,seed /*Not specified? Use for RANDOM seed. */
|
||||
w=12; pad=left('',5) /*width of columnar output; indentation*/
|
||||
dash='─'; @b="biased"; @ub='un'@b /*literals for the SAY column headers. */
|
||||
say pad c('N',5) c(@b) c(@b'%') c(@ub) c(@ub"%") c('samples') /*six column header.*/
|
||||
parse arg # R seed . /*obtain optional arguments from the CL*/
|
||||
if #=='' | #=="," then #=1000 /*#: the number of SAMPLES to be used.*/
|
||||
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")
|
||||
dash=
|
||||
do N=3 to R; b=0; u=0; do j=1 for #; b=b+randN(N)
|
||||
u=u+unbiased()
|
||||
end /*j*/
|
||||
say pad c(N,5) c(b) pct(b) c(u) pct(u) c(#)
|
||||
end /*N*/
|
||||
do N=3 to R; b=0; u=0
|
||||
do j=1 for #; b=b + randN(N); u=u + unbiased()
|
||||
end /*j*/
|
||||
say left('', 5) ctr(N, 5) ctr(b) pct(b) ctr(u) pct(u) ctr(#)
|
||||
end /*N*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
c: return center( arg(1), word(arg(2) w, 1), left(dash, 1) )
|
||||
pct: return c( format(arg(1) / # * 100, , 2)'%' ) /*two decimal digits.*/
|
||||
randN: parse arg z; return random(1, z)==z
|
||||
unbiased: do until x\==randN(N); x=randN(N); end /*until*/; return x
|
||||
ctr: return center( arg(1), word(arg(2) 12, 1), left(dash, 1)) /*show hdr│numbers.*/
|
||||
pct: return ctr( format(arg(1) / # * 100, , 2)'%' ) /*2 decimal digits.*/
|
||||
randN: parse arg z; return random(1, z)==z /*ret 1 if rand==Z.*/
|
||||
unbiased: do until x\==randN(N); x=randN(N); end; return x /* " unbiased RAND*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
fcn randN(N){ (not (0).random(N)).toInt() }
|
||||
fcn unbiased(randN){ while((a:=randN())==randN()){} a }
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
const Z=0d100_000;
|
||||
foreach N in ([3..6]){
|
||||
"%d: biased: %3.2f%%, unbiased: %3.2f%%".fmt(N,
|
||||
(0).reduce(Z,'wrap(s,_){ s+randN(N) },0.0)/Z*100,
|
||||
(0).reduce(Z,'wrap(s,_){ s+unbiased(randN.fp(N)) },0.0)/Z*100)
|
||||
.println();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue