Skip to content

Commit 1ea70f5

Browse files
hemalvarambhiaSergeStinckwich
authored andcommitted
[Issue 105] Math-Random Package Is Messy - Improve Naming, Refactor Tests (#132)
* [issue-105] Removed an obselete test. * [issue-105] Improved the name of the class. The class comment says it is a generator. * [issue-105] Improved the name of the class. * [issue-105] Extracted duplicate code to an intention-revealing method. * [issue-105] Extracted duplicate code to a helper method. * [issue-105] Inlined the method as it's only used in one place. * [issue-105] Improved the name of the method. * [issue-105] Corrected code style. * [issue-105] Improved the name of the method. * [issue-105] Removed obselete code. * [issue-105] Corrected the category. * [issue-105] Improved the names of the methods. * [issue-105] Improved the names of the method. * [issue-105] Removed obselete test. * [issue-105] Brought back a test deleted accidentally. * [issue-105] Made similar code even more similar. * [issue-105] Moved the code to a better place, an intention-revealing method. * [issue-105] Made the code consistent with that of the other tests. This reads a little better now. * [issue-105] Made similar code look the same, improved the name of a test method by using domain language.
1 parent d1175d6 commit 1ea70f5

17 files changed

Lines changed: 164 additions & 174 deletions

src/Math-Random/PMBinomialGenerator.class.st

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ PMBinomialGenerator class >> numberOfTrials: numberOfTrials probabilityOfSuccess
2121
yourself
2222
]
2323

24+
{ #category : #accessing }
25+
PMBinomialGenerator >> expectedValue [
26+
^ numberOfTrials * probability .
27+
]
28+
2429
{ #category : #initialization }
2530
PMBinomialGenerator >> initialize [
2631
self generator: PMParkMillerMinimumRandomGenerator new.

src/Math-Random/PMLehmerRandomGenerator.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PMLehmerRandomGenerator new next
55
"
66
Class {
77
#name : #PMLehmerRandomGenerator,
8-
#superclass : #PMRandomGenerator,
8+
#superclass : #PMPseudoRandomNumberGenerator,
99
#category : #'Math-Random'
1010
}
1111

src/Math-Random/PMLinearCongruentialRandomGenerator.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PMLinearCongruentialRandomGenerator new next.
55
"
66
Class {
77
#name : #PMLinearCongruentialRandomGenerator,
8-
#superclass : #PMRandomGenerator,
8+
#superclass : #PMPseudoRandomNumberGenerator,
99
#category : #'Math-Random'
1010
}
1111

src/Math-Random/PMMarsagliaKissRandom.class.st renamed to src/Math-Random/PMMarsagliaKissRandomGenerator.class.st

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Instance Variables
1010
1111
"
1212
Class {
13-
#name : #PMMarsagliaKissRandom,
14-
#superclass : #PMRandomGenerator,
13+
#name : #PMMarsagliaKissRandomGenerator,
14+
#superclass : #PMPseudoRandomNumberGenerator,
1515
#instVars : [
1616
'kernelRand1',
1717
'kernelRand2'
@@ -20,20 +20,20 @@ Class {
2020
}
2121

2222
{ #category : #'instance creation' }
23-
PMMarsagliaKissRandom class >> default [
23+
PMMarsagliaKissRandomGenerator class >> default [
2424
^self seed: #(
2525
123456789 362436069 521288629 316191069
2626
987654321 458629013 582859209 438195021)
2727
]
2828

2929
{ #category : #'instance creation' }
30-
PMMarsagliaKissRandom class >> new [
30+
PMMarsagliaKissRandomGenerator class >> new [
3131

3232
^ self seed: (PMMarsagliaKissRandomKernel new next: 8)
3333
]
3434

3535
{ #category : #'stream access' }
36-
PMMarsagliaKissRandom >> next [
36+
PMMarsagliaKissRandomGenerator >> next [
3737
"Answer a Float in interval [0.0,1.0) with uniform distribution.
3838
Note that constant 16rFFFFF800 is computed so as to truncate the 64 bits to Float precision.
3939
It is thus ((1 << 32 - 1 << (64 - Float precision) bitAnd: 1 << 32 - 1)) hex"
@@ -42,23 +42,23 @@ PMMarsagliaKissRandom >> next [
4242
]
4343

4444
{ #category : #'stream access' }
45-
PMMarsagliaKissRandom >> peek [
45+
PMMarsagliaKissRandomGenerator >> peek [
4646
^self copy next
4747
]
4848

4949
{ #category : #copying }
50-
PMMarsagliaKissRandom >> postCopy [
50+
PMMarsagliaKissRandomGenerator >> postCopy [
5151
kernelRand1 := kernelRand1 copy.
5252
kernelRand2 := kernelRand2 copy
5353
]
5454

5555
{ #category : #accessing }
56-
PMMarsagliaKissRandom >> seed [
56+
PMMarsagliaKissRandomGenerator >> seed [
5757
^ kernelRand1 seed , kernelRand2 seed
5858
]
5959

6060
{ #category : #accessing }
61-
PMMarsagliaKissRandom >> seed: aWordArray [
61+
PMMarsagliaKissRandomGenerator >> seed: aWordArray [
6262
"Initialize with an Array of eight 32-bits Integer"
6363
kernelRand1 := PMMarsagliaKissRandomKernel seed: (aWordArray first: 4).
6464
kernelRand2 := PMMarsagliaKissRandomKernel seed: (aWordArray last: 4)

src/Math-Random/PMMersenneTwisterRandomGenerator.class.st

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ MersenneTwisterRandom new nextInteger.
1111
"
1212
Class {
1313
#name : #PMMersenneTwisterRandomGenerator,
14-
#superclass : #PMRandomGenerator,
14+
#superclass : #PMPseudoRandomNumberGenerator,
1515
#instVars : [
1616
'states',
1717
'mti'
@@ -26,7 +26,7 @@ Class {
2626
'TemperingMaskB',
2727
'TemperingMaskC'
2828
],
29-
#category : 'Math-Random'
29+
#category : #'Math-Random'
3030
}
3131

3232
{ #category : #'class initialization' }

src/Math-Random/PMParkMillerMinimumRandomGenerator.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PMParkMillerMinimumRandomGenerator new next
55
"
66
Class {
77
#name : #PMParkMillerMinimumRandomGenerator,
8-
#superclass : #PMRandomGenerator,
8+
#superclass : #PMPseudoRandomNumberGenerator,
99
#category : #'Math-Random'
1010
}
1111

src/Math-Random/PMRandomGenerator.class.st renamed to src/Math-Random/PMPseudoRandomNumberGenerator.class.st

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ And finally, there's a taxonomy more related with the internal implementation, b
3131
3232
"
3333
Class {
34-
#name : #PMRandomGenerator,
34+
#name : #PMPseudoRandomNumberGenerator,
3535
#superclass : #Object,
3636
#instVars : [
3737
'seed'
3838
],
39-
#category : 'Math-Random'
39+
#category : #'Math-Random'
4040
}
4141

4242
{ #category : #testing }
43-
PMRandomGenerator class >> chiSquare: range repeating: anInteger [
43+
PMPseudoRandomNumberGenerator class >> chiSquare: range repeating: anInteger [
4444
"Run a 'chi-square' test on a random number generator, over a range of integers given by range, repeating anInteger times.
4545
Answer an Array containing the result of the chi-square test (which should be the same as the range), and the upper and lower bounds for 'good' randomness (assuming that anInteger is at least 10 * range)."
4646

@@ -64,29 +64,29 @@ PMRandomGenerator class >> chiSquare: range repeating: anInteger [
6464
]
6565

6666
{ #category : #'instance creation' }
67-
PMRandomGenerator class >> new [
67+
PMPseudoRandomNumberGenerator class >> new [
6868
"Answer a new instance of the receiver"
6969

7070
^ (self seed: Time millisecondClockValue) initialize
7171
]
7272

7373
{ #category : #'instance creation' }
74-
PMRandomGenerator class >> seed: anInteger [
74+
PMPseudoRandomNumberGenerator class >> seed: anInteger [
7575
"Anwer a new Random stream with the initial seed anInteger."
7676

7777
^ self basicNew seed: anInteger; yourself
7878
]
7979

8080
{ #category : #testing }
81-
PMRandomGenerator >> atEnd [
81+
PMPseudoRandomNumberGenerator >> atEnd [
8282
"Answer whether the receiver is at its end - never true for a stream of Random numbers"
8383

8484
^ false
8585

8686
]
8787

8888
{ #category : #accessing }
89-
PMRandomGenerator >> contents [
89+
PMPseudoRandomNumberGenerator >> contents [
9090
"Answer all of the objects in the collection accessed by the receiver.
9191
Implementation Notes: Random streams are infinite, so there is no implementation possible."
9292

@@ -95,52 +95,52 @@ PMRandomGenerator >> contents [
9595
]
9696

9797
{ #category : #testing }
98-
PMRandomGenerator >> isReadable [
98+
PMPseudoRandomNumberGenerator >> isReadable [
9999
"Answer whether the receiver can be read from (i.e. it implements the gettableStream protocol)."
100100

101101
^ true
102102

103103
]
104104

105105
{ #category : #testing }
106-
PMRandomGenerator >> isWriteable [
106+
PMPseudoRandomNumberGenerator >> isWriteable [
107107
"Answer whether the receiver can be written to (i.e. it implements the puttableStream protocol)."
108108

109109
^ false
110110

111111
]
112112

113113
{ #category : #'stream access' }
114-
PMRandomGenerator >> next [
114+
PMPseudoRandomNumberGenerator >> next [
115115
"Answer a pseudo-Random number"
116116

117117
^ self subclassResponsibility
118118
]
119119

120120
{ #category : #'stream access' }
121-
PMRandomGenerator >> next: anInteger [
121+
PMPseudoRandomNumberGenerator >> next: anInteger [
122122
"Answer a collection of size anInteger of pseudo-random Floats between 0 and 1. "
123123

124124
^ ( 1 to: anInteger ) collect: [ :i | self next ]
125125

126126
]
127127

128128
{ #category : #'stream access' }
129-
PMRandomGenerator >> peek [
129+
PMPseudoRandomNumberGenerator >> peek [
130130
"Answer a pseudo-Random number generated from the next seed, but do not advance down the stream (i.e. self peek = self peek). "
131131

132132
^ self subclassResponsibility
133133
]
134134

135135
{ #category : #accessing }
136-
PMRandomGenerator >> seed [
136+
PMPseudoRandomNumberGenerator >> seed [
137137
"Returns the instance var of the receiver for external manipulation"
138138

139139
^ seed
140140
]
141141

142142
{ #category : #accessing }
143-
PMRandomGenerator >> seed: anObject [
143+
PMPseudoRandomNumberGenerator >> seed: anObject [
144144
"Sets the instance var of the receiver with anObject. (No checking is done)"
145145

146146
seed := anObject

src/Math-Tests-Random/PMBernoulliGeneratorTest.class.st

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ Class {
77
#category : #'Math-Tests-Random'
88
}
99

10-
{ #category : #tests }
11-
PMBernoulliGeneratorTest >> testGenerator [
12-
| g bern |
13-
g := PMLinearCongruentialRandomGenerator new.
14-
bern := PMBernoulliGenerator new.
15-
self
16-
assert: (bern generator isKindOf: PMBernoulliGenerator defaultGeneratorClass).
17-
bern generator: g.
18-
self assert: (bern generator isKindOf: PMLinearCongruentialRandomGenerator)
19-
]
20-
2110
{ #category : #tests }
2211
PMBernoulliGeneratorTest >> testNextYieldsOneOrZero [
2312
| gen |

src/Math-Tests-Random/PMBinomialGeneratorTest.class.st

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,6 @@ Class {
44
#category : #'Math-Tests-Random'
55
}
66

7-
{ #category : #tests }
8-
PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [
9-
"Its purpose is to verify correct convergence of the binomial distribution,
10-
should be Normal (np, np(1-p))"
11-
12-
| gen nums mean probabilityOfSuccess numberOfTrials |
13-
probabilityOfSuccess := (Random seed: 0.0) next sqrt.
14-
numberOfTrials := 1000.
15-
gen := PMBinomialGenerator
16-
numberOfTrials: numberOfTrials
17-
probabilityOfSuccess: probabilityOfSuccess.
18-
19-
nums := OrderedCollection new.
20-
(1 to: numberOfTrials) do: [ :ea | nums add: gen next ].
21-
22-
mean := numberOfTrials * probabilityOfSuccess.
23-
self assert: nums min > (mean * (1 - 0.2)).
24-
self assert: nums max < (mean * (1 + 0.2)).
25-
self assert: (nums average - mean) abs < 5
26-
]
27-
287
{ #category : #tests }
298
PMBinomialGeneratorTest >> testBinomialGeneratorWithSuccessProbabilityOfOneAlwaysReturnNumberOfTrials [
309
| g numberOfTrials |
@@ -56,3 +35,23 @@ PMBinomialGeneratorTest >> testBinomialGeneratorWithSuccessProbabilityOfZeroAlwa
5635
g generator: PMMersenneTwisterRandomGenerator new.
5736
self assert: g next equals: 0
5837
]
38+
39+
{ #category : #tests }
40+
PMBinomialGeneratorTest >> testSampleMeanConvergesToExpectedValue [
41+
"Its purpose is to verify correct convergence of the binomial distribution,
42+
should be Normal (np, np(1-p))"
43+
44+
| gen sample probabilityOfSuccess numberOfTrials |
45+
probabilityOfSuccess := (Random seed: 0.0) next sqrt.
46+
numberOfTrials := 1000.
47+
gen := PMBinomialGenerator
48+
numberOfTrials: numberOfTrials
49+
probabilityOfSuccess: probabilityOfSuccess.
50+
51+
sample := OrderedCollection new.
52+
numberOfTrials timesRepeat: [ sample add: gen next ].
53+
54+
self assert: sample min > (gen expectedValue * (1 - 0.2)).
55+
self assert: sample max < (gen expectedValue * (1 + 0.2)).
56+
self assert: (sample average - gen expectedValue) abs < 5
57+
]

src/Math-Tests-Random/PMExponentialGeneratorTest.class.st

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ PMExponentialGeneratorTest >> testGenerator [
1313
eg := PMExponentialGenerator new.
1414
self
1515
assert: (eg generator isKindOf: PMExponentialGenerator defaultGeneratorClass).
16-
self assert: (eg generator isKindOf: PMRandomGenerator).
16+
self assert: (eg generator isKindOf: PMPseudoRandomNumberGenerator).
1717
eg generator: PMMersenneTwisterRandomGenerator new.
1818
self assert: (eg generator isKindOf: PMMersenneTwisterRandomGenerator).
1919
self assert: (eg next isKindOf: Number)
2020
]
2121

2222
{ #category : #tests }
23-
PMExponentialGeneratorTest >> testPeekAlwaysAnswersTheSame [
23+
PMExponentialGeneratorTest >> testPeekIsIdempotent [
2424
| eg |
2525
eg := PMExponentialGenerator new.
2626
self assert: eg peek equals: eg peek.
@@ -31,10 +31,11 @@ PMExponentialGeneratorTest >> testPeekAlwaysAnswersTheSame [
3131
PMExponentialGeneratorTest >> testSampleMeanConvergesToDistributionMean [
3232
"testing a random sample seems suspect. We use a 5% interval here"
3333

34-
| eg arr |
35-
eg := PMExponentialGenerator mean: 10.
36-
arr := Array new: 10000.
37-
arr := arr collect: [ :each | eg next ].
34+
| gen sample |
35+
gen := PMExponentialGenerator mean: 10.
36+
sample := Array new: 10000.
37+
sample := sample collect: [ :each | gen next ].
38+
3839
self
39-
assert: (arr average between: eg mean * 0.95 and: eg mean * 1.05)
40+
assert: (sample average between: gen mean * 0.95 and: gen mean * 1.05)
4041
]

0 commit comments

Comments
 (0)