Skip to content

Commit d185377

Browse files
authored
Merge pull request #273 from olekscode/269-Remove-random-methods
Fixed 269. Remove random methods from Number and Integer
2 parents ad0cfc0 + 37577d1 commit d185377

25 files changed

+326
-221
lines changed

src/Math-Chromosome/PMChromosomeManager.class.st

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ Class {
55
'population',
66
'populationSize',
77
'rateOfMutation',
8-
'rateOfCrossover'
8+
'rateOfCrossover',
9+
'randomNumberGenerator'
910
],
10-
#category : 'Math-Chromosome'
11+
#category : #'Math-Chromosome'
1112
}
1213

1314
{ #category : #creation }
@@ -28,6 +29,12 @@ PMChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
2829
^self subclassResponsibility
2930
]
3031

32+
{ #category : #initialization }
33+
PMChromosomeManager >> initialize [
34+
super initialize.
35+
randomNumberGenerator := Random new.
36+
]
37+
3138
{ #category : #information }
3239
PMChromosomeManager >> isFullyPopulated [
3340

@@ -56,7 +63,7 @@ PMChromosomeManager >> populationSize: anInteger [
5663
PMChromosomeManager >> process: aChromosome1 and: aChromosome2 [
5764

5865
| roll |
59-
roll := Number random.
66+
roll := randomNumberGenerator next.
6067
roll < rateOfCrossover
6168
ifTrue: [population addAll: (self crossover: aChromosome1 and: aChromosome2)]
6269
ifFalse:

src/Math-Chromosome/PMVectorChromosomeManager.class.st

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@ Class {
55
'origin',
66
'range'
77
],
8-
#category : 'Math-Chromosome'
8+
#category : #'Math-Chromosome'
99
}
1010

1111
{ #category : #operation }
1212
PMVectorChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
1313

14-
| index new1 new2|
15-
index := ( aChromosome1 size - 1) random + 2.
14+
| index new1 new2 |
15+
16+
index := randomNumberGenerator nextIntegerBetween: 2 and: aChromosome1 size.
17+
1618
new1 := self clone: aChromosome1.
1719
new1 replaceFrom: index to: new1 size with: aChromosome2 startingAt: index.
20+
1821
new2 := self clone: aChromosome2.
1922
new2 replaceFrom: index to: new2 size with: aChromosome1 startingAt: index.
20-
^Array with: new1 with: new2
23+
24+
^ Array with: new1 with: new2
2125
]
2226

2327
{ #category : #operation }
2428
PMVectorChromosomeManager >> mutate: aVector [
2529

2630
| index |
27-
index := aVector size random + 1.
31+
index := randomNumberGenerator nextIntegerBetween: 1 and: aVector size.
32+
2833
^( aVector copy)
29-
at: index put: ( self randomComponent: index);
34+
at: index put: (self randomComponent: index);
3035
yourself
3136
]
3237

@@ -39,13 +44,13 @@ PMVectorChromosomeManager >> origin: aVector [
3944
{ #category : #creation }
4045
PMVectorChromosomeManager >> randomChromosome [
4146

42-
^( ( 1 to: origin size) collect: [ :n | self randomComponent: n]) asPMVector
47+
^ ((1 to: origin size) collect: [ :n | self randomComponent: n ]) asPMVector
4348
]
4449

4550
{ #category : #information }
4651
PMVectorChromosomeManager >> randomComponent: anInteger [
4752

48-
^( range at: anInteger) asFloat random + ( origin at: anInteger)
53+
^ (randomNumberGenerator nextBetween: 0 and: (range at: anInteger)) + (origin at: anInteger)
4954
]
5055

5156
{ #category : #initialization }

src/Math-Complex/PMComplex.class.st

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ PMComplex class >> one [
102102

103103
{ #category : #'*Math-Complex' }
104104
PMComplex class >> random [
105-
"Answers a random number with abs between 0 and 1."
106-
107-
^ self abs: 1.0 random arg: 2 * Float pi random
105+
"Answers a random number with abs between 0 and 1."
106+
| random |
107+
random := Random new.
108+
109+
^ self
110+
abs: random next
111+
arg: 2 * (random nextBetween: 0 and: Float pi)
108112
]
109113

110114
{ #category : #'instance creation' }

src/Math-Complex/PMComplex.extension.st

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ PMComplex >> productWithVector: aVector [
1717
^ aVector collect: [ :each | each * self ]
1818
]
1919

20-
{ #category : #'*Math-Complex' }
21-
PMComplex class >> random [
22-
"Answers a random number with abs between 0 and 1."
23-
24-
^ self abs: 1.0 random arg: 2 * Float pi random
25-
]
26-
2720
{ #category : #'*Math-Complex' }
2821
PMComplex >> random [
2922
"analog to Number>>random. However, the only bound is that the abs of the produced complex is less than the length of the receive. The receiver effectively defines a disc within which the random element can be produced."
3023
^ self class random * self
3124

3225
]
3326

27+
{ #category : #'*Math-Complex' }
28+
PMComplex class >> random [
29+
"Answers a random number with abs between 0 and 1."
30+
| random |
31+
random := Random new.
32+
33+
^ self
34+
abs: random next
35+
arg: 2 * (random nextBetween: 0 and: Float pi)
36+
]
37+
3438
{ #category : #'*Math-Complex' }
3539
PMComplex >> subtractToPolynomial: aPolynomial [
3640
^ aPolynomial addNumber: self negated

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ Class {
1313

1414
{ #category : #information }
1515
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.
16+
| random v1 v2 w y |
17+
random := Random new.
18+
19+
[
20+
v1 :=random next * 2 - 1.
21+
v2 := random next * 2 - 1.
22+
w := v1 squared + v2 squared.
23+
w > 1
24+
] whileTrue.
25+
26+
y := (w ln * 2 negated / w) sqrt.
27+
v1 := y * v1.
28+
NextRandom := y * v2.
29+
^v1.
2530
]
2631

2732
{ #category : #information }

src/Math-Core/PMVector.class.st

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ Class {
2727
PMVector class >> new: size random: maxRandomNumber [
2828
"Answer an instance of me, with number of elements equal to size, each
2929
a randomNumber lower than maxRandomNumber ."
30+
31+
| random |
32+
random := Random new.
3033

31-
^self newFrom: ( (1 to: size ) collect: [:e|maxRandomNumber random ] )
34+
^ self newFrom: ((1 to: size ) collect: [ :each |
35+
random nextBetween: 0 and: maxRandomNumber ]).
3236
]
3337

3438
{ #category : #private }
@@ -43,10 +47,14 @@ PMVector class >> ones: anInteger [
4347
{ #category : #private }
4448
PMVector class >> randomSize: anInteger maxNumber: aMaxNumber [
4549
"Creates a vector of rand numbers from 0 to aMaxNumber."
46-
|a|
47-
a := PMVector new: anInteger.
48-
1 to: a size do: [ :n | a at: n put: (aMaxNumber random)].
49-
^a
50+
| random vector |
51+
random := Random new.
52+
53+
vector := PMVector new: anInteger.
54+
1 to: vector size do: [ :i |
55+
vector at: i put: (random nextBetween: 0 and: aMaxNumber)].
56+
57+
^ vector
5058
]
5159

5260
{ #category : #private }

src/Math-FunctionFit/PMAnotherChromosomeManager.class.st

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Class {
1313
#classVars : [
1414
'Primes'
1515
],
16-
#category : 'Math-FunctionFit'
16+
#category : #'Math-FunctionFit'
1717
}
1818

1919
{ #category : #utils }
@@ -34,18 +34,27 @@ PMAnotherChromosomeManager class >> integerDigitsFor: anInteger base: aBase [
3434

3535
{ #category : #utils }
3636
PMAnotherChromosomeManager class >> numberOfHamersleyPoints: n dimension: d randomized: aBoolean [
37-
"a bit randomized "
38-
|dist|
39-
dist :=1.0 / n.
40-
^(1 to: n) collect: [:number| (1 to: d)collect: [:dim|
41-
( dim=1)
42-
ifTrue:[aBoolean ifTrue: [(number/n)- dist random] ifFalse: [(number/n)]]
43-
ifFalse: [|sum prime|
44-
sum :=0.
45-
prime :=Primes at: dim-1.
46-
(self integerDigitsFor: number base: prime)
47-
reverse withIndexDo: [:i :index|sum:= i/(prime raisedToInteger: index)+sum.].
48-
aBoolean ifTrue: [sum + dist random] ifFalse: [sum] ] ]].
37+
"a bit randomized "
38+
| dist randomNumberGenerator |
39+
dist := 1.0 / n.
40+
randomNumberGenerator := Random new.
41+
42+
^(1 to: n) collect: [ :number |
43+
(1 to: d) collect: [ :dim |
44+
(dim=1)
45+
ifTrue: [ aBoolean
46+
ifTrue: [ (number/n) - (randomNumberGenerator nextBetween: 0 and: dist) ]
47+
ifFalse: [ number/n ] ]
48+
ifFalse: [ |sum prime|
49+
sum := 0.
50+
prime := Primes at: dim - 1.
51+
52+
(self integerDigitsFor: number base: prime) reverse withIndexDo: [ :i :index |
53+
sum := i / (prime raisedToInteger: index) + sum ].
54+
55+
aBoolean
56+
ifTrue: [ sum + (randomNumberGenerator nextBetween: 0 and: dist) ]
57+
ifFalse: [ sum ] ] ]].
4958
]
5059

5160
{ #category : #'instance creation' }
@@ -57,27 +66,44 @@ PMAnotherChromosomeManager class >> origin: anArray range: anotherArray [
5766
PMAnotherChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
5867
"the Discrete Recombination operator
5968
that does not prefer schemata of certain parameters based on their position"
60-
|new1 new2|
61-
aChromosome1=aChromosome2 ifTrue:[^Array with:( self mutate: aChromosome2) with: (self mutate: aChromosome1) ].
69+
| new1 new2 |
70+
71+
aChromosome1 = aChromosome2 ifTrue:[
72+
^ Array
73+
with: (self mutate: aChromosome2)
74+
with: (self mutate: aChromosome1) ].
75+
6276
new1 := self clone: aChromosome1.
6377
new2 := self clone: aChromosome2.
64-
2 to: new1 size do: [:i| (Float random <0.5)ifTrue:[
78+
79+
2 to: new1 size do: [ :i |
80+
(randomNumberGenerator next < 0.5) ifTrue: [
6581
new1 at: i put: (aChromosome2 at: i).
66-
new2 at: i put: (aChromosome1 at: i)]].
67-
^Array with: new1 with: new2
82+
new2 at: i put: (aChromosome1 at: i) ] ].
83+
84+
^ Array with: new1 with: new2
6885
]
6986

7087
{ #category : #operation }
7188
PMAnotherChromosomeManager >> eirCrossover: aChromosome1 and: aChromosome2 [
72-
"the Extended Intermediate Recombination 0.5 operator, slightly changed to make it more similar to linecrossover (distribution is more centered around Chromosome1, which is better than C2)"
73-
| r new1 new2 dif|
74-
dif:=aChromosome2 - aChromosome1.
75-
dif norm=0 ifTrue:[^Array with:( self mutate: aChromosome2) with: (self mutate: aChromosome1) ].
76-
r:=(1 to: aChromosome1 size) collect:[:i| 2.0 random -0.5] .
77-
new1 := aChromosome1+ (r * dif).
78-
r:=(1 to: aChromosome1 size) collect:[:i| 1.0 random -0.5] .
79-
new2 := aChromosome1+ (r * dif).
80-
^Array with: new1 with: new2
89+
"the Extended Intermediate Recombination 0.5 operator, slightly changed to make it more similar to linecrossover (distribution is more centered around Chromosome1, which is better than C2)"
90+
| randomNumbers new1 new2 dif |
91+
dif := aChromosome2 - aChromosome1.
92+
93+
dif norm = 0 ifTrue: [
94+
^ { self mutate: aChromosome2 . self mutate: aChromosome1 } ].
95+
96+
randomNumbers := (1 to: aChromosome1 size) collect: [ :i |
97+
randomNumberGenerator nextBetween: -0.5 and: 1.5 ].
98+
99+
new1 := aChromosome1 + (randomNumbers * dif).
100+
101+
randomNumbers := (1 to: aChromosome1 size) collect: [ :i |
102+
randomNumberGenerator nextBetween: -0.5 and: 0.5 ].
103+
104+
new2 := aChromosome1 + (randomNumbers * dif).
105+
106+
^ { new1 . new2 }
81107

82108
]
83109

@@ -116,20 +142,25 @@ PMAnotherChromosomeManager >> lineCrossOver: aChromosome1 and: aChromosome2 [
116142

117143
{ #category : #operation }
118144
PMAnotherChromosomeManager >> mutate: aVector [
119-
"BGA mutation"
120-
| isMutated threshold new index|
121-
isMutated :=false.
122-
threshold :=1/ aVector size asFloat .
123-
new :=aVector copy.
124-
1 to: aVector size do:[:i| Float random < threshold ifTrue:[
125-
isMutated :=true.
145+
"BGA mutation"
146+
| isMutated threshold new index |
147+
148+
isMutated := false.
149+
threshold := 1 / aVector size asFloat.
150+
new := aVector copy.
151+
152+
1 to: aVector size do: [ :i |
153+
randomNumberGenerator next < threshold ifTrue: [
154+
isMutated := true.
126155
new at: i put: (new at: i) +
127-
((Float random <0.5 ifTrue: [0.5] ifFalse:[0.5 negated ] )*(self randomRangeAt: i))]].
156+
((randomNumberGenerator next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: i)) ] ].
157+
128158
isMutated ifFalse: [
129-
index := aVector size random + 1.
159+
index := randomNumberGenerator nextIntegerBetween: 1 and: aVector size.
130160
new at: index put: (new at: index) +
131-
((Float random <0.5 ifTrue: [0.5] ifFalse:[0.5 negated ] )*(self randomRangeAt: index))].
132-
^new
161+
((randomNumberGenerator next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: index)) ].
162+
163+
^ new
133164
]
134165

135166
{ #category : #accessing }
@@ -156,7 +187,7 @@ PMAnotherChromosomeManager >> printOn: aStream [
156187
{ #category : #operation }
157188
PMAnotherChromosomeManager >> process: aChromosome1 and: aChromosome2 [
158189
| roll |
159-
roll := Number random.
190+
roll := randomNumberGenerator next.
160191
roll < rateOfCrossover
161192
ifTrue: [population addAll: (self crossover: aChromosome1 and: aChromosome2)]
162193
ifFalse:
@@ -224,8 +255,8 @@ PMAnotherChromosomeManager >> rateOfMutation: aNumber [
224255

225256
{ #category : #private }
226257
PMAnotherChromosomeManager >> smallDistribution [
227-
"an exponential distribution as used by H. Mühlenbein"
228-
^2 raisedTo: (16 *Float random negated)
258+
"an exponential distribution as used by H. Mühlenbein"
259+
^ 2 raisedTo: (16 * randomNumberGenerator next negated)
229260
]
230261

231262
{ #category : #private }

src/Math-Matrix/PMMatrix.class.st

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ PMMatrix class >> rows: nRows columns: nCols element: fillElement [
146146
]
147147

148148
{ #category : #'as yet unclassified' }
149-
PMMatrix class >> rows: rows columns: columns random: aMaxNumber [
149+
PMMatrix class >> rows: aNumberOfRows columns: aNumberOfColumns random: aMaxNumber [
150150
"Answer a new Matrix of the given dimensions filled with random numbers"
151-
|a b|
152-
a:= (1 to: rows) collect: [:row |b:=PMVector new:columns .
153-
1 to: columns do: [:column |
154-
b at: column put: (aMaxNumber random)].
155-
b].
156-
^PMMatrix rows: a
151+
| random rows |
152+
random := Random new.
153+
154+
rows := (1 to: aNumberOfRows) collect: [ :i |
155+
(1 to: aNumberOfColumns) collect: [ :j |
156+
random nextBetween: 0 and: aMaxNumber ] ].
157+
158+
^ self rows: rows
157159
]
158160

159161
{ #category : #'instance creation' }

0 commit comments

Comments
 (0)