Skip to content

Commit ea87d33

Browse files
Extract boxMullerTransform to be more intuition revealing
1 parent c83319d commit ea87d33

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/Math-Core-Distribution/PMNormalDistribution.class.st

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ Class {
1111
#category : #'Math-Core-Distribution'
1212
}
1313

14+
{ #category : #information }
15+
PMNormalDistribution class >> boxMullerTransform [
16+
|v1 v2 w y|
17+
[ v1 := Number random * 2 - 1.
18+
v2 := Number random * 2 - 1.
19+
w := v1 squared + v2 squared.
20+
w > 1 ] whileTrue.
21+
y := (w ln * 2 negated / w) sqrt.
22+
v1 := y * v1.
23+
NextRandom := y * v2.
24+
^v1.
25+
]
26+
1427
{ #category : #information }
1528
PMNormalDistribution class >> distributionName [
1629

@@ -42,17 +55,11 @@ PMNormalDistribution class >> new: aNumber1 sigma: aNumber2 [
4255
PMNormalDistribution class >> random [
4356
"Answer a random number distributed according to a (0,1) normal distribution."
4457

45-
| v1 v2 w y |
58+
| v1 |
4659
NextRandom
4760
ifNil: [
48-
"Use Box-Muller transform"
49-
[ v1 := Number random * 2 - 1.
50-
v2 := Number random * 2 - 1.
51-
w := v1 squared + v2 squared.
52-
w > 1 ] whileTrue.
53-
y := (w ln * 2 negated / w) sqrt.
54-
v1 := y * v1.
55-
NextRandom := y * v2 ]
61+
62+
v1 := self boxMullerTransform ]
5663
ifNotNil: [ v1 := NextRandom.
5764
NextRandom := nil ].
5865
^ v1

0 commit comments

Comments
 (0)