34 lines
719 B
Text
34 lines
719 B
Text
|
|
Method "U(_,_)" is
|
|||
|
|
[
|
|||
|
|
lower : number,
|
|||
|
|
upper : number
|
|||
|
|
|
|
|||
|
|
divisor ::= ((1<<32)) ÷ (upper - lower)→double;
|
|||
|
|
map a pRNG through [i : integer | (i ÷ divisor) + lower]
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
Method "a Marsaglia polar sampler" is
|
|||
|
|
[
|
|||
|
|
generator for
|
|||
|
|
[
|
|||
|
|
yield : [double]→⊤
|
|||
|
|
|
|
|||
|
|
source ::= U(-1, 1);
|
|||
|
|
Repeat [
|
|||
|
|
x ::= take 1 from source[1];
|
|||
|
|
y ::= take 1 from source[1];
|
|||
|
|
s ::= x^2 + y^2;
|
|||
|
|
If 0 < s < 1 then
|
|||
|
|
[
|
|||
|
|
factor ::= ((-2 × ln s) ÷ s) ^ 0.5;
|
|||
|
|
yield(x × factor);
|
|||
|
|
yield(y × factor);
|
|||
|
|
];
|
|||
|
|
]
|
|||
|
|
]
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
// the default distribution has mean 0 and std dev 1.0, so we scale the values
|
|||
|
|
sampler ::= map a Marsaglia polar sampler through [d : double | d ÷ 2.0 + 1.0];
|
|||
|
|
values ::= take 1000 from sampler;
|