Skip to content

Commit 8e3839d

Browse files
committed
Removed some references to random
1 parent 497c9f6 commit 8e3839d

File tree

10 files changed

+127
-75
lines changed

10 files changed

+127
-75
lines changed

src/Math-Chromosome/PMChromosomeManager.class.st

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Class {
77
'rateOfMutation',
88
'rateOfCrossover'
99
],
10-
#category : 'Math-Chromosome'
10+
#category : #'Math-Chromosome'
1111
}
1212

1313
{ #category : #creation }
@@ -55,8 +55,9 @@ PMChromosomeManager >> populationSize: anInteger [
5555
{ #category : #operation }
5656
PMChromosomeManager >> process: aChromosome1 and: aChromosome2 [
5757

58-
| roll |
59-
roll := Number random.
58+
| random roll |
59+
random := Random new.
60+
roll := random next.
6061
roll < rateOfCrossover
6162
ifTrue: [population addAll: (self crossover: aChromosome1 and: aChromosome2)]
6263
ifFalse:

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-FunctionFit/PMAnotherChromosomeManager.class.st

Lines changed: 39 additions & 20 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 }
@@ -57,14 +57,24 @@ PMAnotherChromosomeManager class >> origin: anArray range: anotherArray [
5757
PMAnotherChromosomeManager >> crossover: aChromosome1 and: aChromosome2 [
5858
"the Discrete Recombination operator
5959
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) ].
60+
| random new1 new2 |
61+
62+
aChromosome1 = aChromosome2 ifTrue:[
63+
^ Array
64+
with: (self mutate: aChromosome2)
65+
with: (self mutate: aChromosome1) ].
66+
6267
new1 := self clone: aChromosome1.
6368
new2 := self clone: aChromosome2.
64-
2 to: new1 size do: [:i| (Float random <0.5)ifTrue:[
69+
70+
random := Random new.
71+
72+
2 to: new1 size do: [ :i |
73+
(random next < 0.5) ifTrue: [
6574
new1 at: i put: (aChromosome2 at: i).
66-
new2 at: i put: (aChromosome1 at: i)]].
67-
^Array with: new1 with: new2
75+
new2 at: i put: (aChromosome1 at: i) ] ].
76+
77+
^ Array with: new1 with: new2
6878
]
6979

7080
{ #category : #operation }
@@ -116,20 +126,26 @@ PMAnotherChromosomeManager >> lineCrossOver: aChromosome1 and: aChromosome2 [
116126

117127
{ #category : #operation }
118128
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.
129+
"BGA mutation"
130+
| isMutated threshold new random index|
131+
132+
isMutated := false.
133+
threshold := 1 / aVector size asFloat.
134+
new := aVector copy.
135+
random := Random new.
136+
137+
1 to: aVector size do: [ :i |
138+
random next < threshold ifTrue: [
139+
isMutated := true.
126140
new at: i put: (new at: i) +
127-
((Float random <0.5 ifTrue: [0.5] ifFalse:[0.5 negated ] )*(self randomRangeAt: i))]].
141+
((random next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: i)) ] ].
142+
128143
isMutated ifFalse: [
129144
index := aVector size random + 1.
130145
new at: index put: (new at: index) +
131-
((Float random <0.5 ifTrue: [0.5] ifFalse:[0.5 negated ] )*(self randomRangeAt: index))].
132-
^new
146+
((random next < 0.5 ifTrue: [ 0.5 ] ifFalse:[ -0.5 ]) * (self randomRangeAt: index)) ].
147+
148+
^ new
133149
]
134150

135151
{ #category : #accessing }
@@ -155,8 +171,9 @@ PMAnotherChromosomeManager >> printOn: aStream [
155171

156172
{ #category : #operation }
157173
PMAnotherChromosomeManager >> process: aChromosome1 and: aChromosome2 [
158-
| roll |
159-
roll := Number random.
174+
| random roll |
175+
random := Random new.
176+
roll := random next.
160177
roll < rateOfCrossover
161178
ifTrue: [population addAll: (self crossover: aChromosome1 and: aChromosome2)]
162179
ifFalse:
@@ -224,8 +241,10 @@ PMAnotherChromosomeManager >> rateOfMutation: aNumber [
224241

225242
{ #category : #private }
226243
PMAnotherChromosomeManager >> smallDistribution [
227-
"an exponential distribution as used by H. Mühlenbein"
228-
^2 raisedTo: (16 *Float random negated)
244+
"an exponential distribution as used by H. Mühlenbein"
245+
| random |
246+
random := Random new.
247+
^ 2 raisedTo: (16 * random next negated)
229248
]
230249

231250
{ #category : #private }

src/Math-Numerical/PMBisectionZeroFinder.class.st

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ PMBisectionZeroFinder >> evaluateIteration [
3232

3333
{ #category : #operation }
3434
PMBisectionZeroFinder >> findNegativeXFrom: aNumber1 range: aNumber2 [
35-
| n |
35+
| random n |
3636
n := 0.
37-
[ negativeX := Number random * aNumber2 + aNumber1.
37+
random := Random new.
38+
39+
[ negativeX := random next * aNumber2 + aNumber1.
3840
( functionBlock value: negativeX) < 0
3941
] whileFalse: [ n := n + 0.1.
4042
n > maximumIterations
@@ -44,9 +46,11 @@ PMBisectionZeroFinder >> findNegativeXFrom: aNumber1 range: aNumber2 [
4446

4547
{ #category : #operation }
4648
PMBisectionZeroFinder >> findPositiveXFrom: aNumber1 range: aNumber2 [
47-
| n |
49+
| n random |
4850
n := 0.
49-
[ positiveX := Number random * aNumber2 + aNumber1.
51+
random := Random new.
52+
53+
[ positiveX := random next * aNumber2 + aNumber1.
5054
( functionBlock value: positiveX) > 0
5155
] whileFalse: [ n := n + 1.
5256
n > maximumIterations

src/Math-Numerical/PMGeneticOptimizer.class.st

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ PMGeneticOptimizer >> processRandomParents: aNumberArray [
7777
{ #category : #information }
7878
PMGeneticOptimizer >> randomIndex: aNumberArray [
7979
"Private"
80-
| x n |
81-
x := Number random.
80+
| random x n |
81+
random := Random new.
82+
x := random next.
8283
n := 1.
8384
aNumberArray do:
8485
[ :each |

src/Math-Numerical/PMNewtonZeroFinder.class.st

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,22 @@ PMNewtonZeroFinder class >> function: aBlock1 derivative: aBlock2 [
4343
PMNewtonZeroFinder >> computeInitialValues [
4444
"Private - If no derivative has been defined, take an ad-hoc definition.
4545
If no initial value has been defined, take 0 as the starting point (for lack of anything better)."
46-
| n |
47-
result isNil
48-
ifTrue: [ result := 0].
49-
derivativeBlock isNil
50-
ifTrue: [ derivativeBlock := self defaultDerivativeBlock].
46+
| n random |
47+
48+
result ifNil: [ result := 0].
49+
derivativeBlock ifNil: [ derivativeBlock := self defaultDerivativeBlock].
50+
5151
n := 0.
52-
[ (derivativeBlock value: result) equalsTo: 0]
53-
whileTrue: [ n := n + 1.
54-
n > maximumIterations
55-
ifTrue: [ self error: 'Function''s derivative seems to be zero everywhere'].
56-
result := Number random + result].
52+
random := Random new.
53+
54+
[ (derivativeBlock value: result) equalsTo: 0] whileTrue: [
55+
n := n + 1.
56+
57+
n > maximumIterations ifTrue: [
58+
self error: 'Function''s derivative seems to be zero everywhere' ].
59+
60+
result := random next + result ].
61+
5762
newFunctionValue := functionBlock value: result.
5863
]
5964

@@ -105,16 +110,22 @@ PMNewtonZeroFinder >> initialize [
105110
PMNewtonZeroFinder >> setDerivative: aBlock [
106111
"Defines the derivative of the function for which zeroes will be found.
107112
Tests if provided block indeed implements the derivative of the function"
108-
| x |
109-
(aBlock respondsTo: #value:)
110-
ifFalse: [self error: 'Derivative block must implement the method value:'].
111-
x := result isNil
112-
ifTrue: [Number random]
113-
ifFalse: [result + Number random].
113+
| x random |
114+
115+
(aBlock respondsTo: #value:) ifFalse: [
116+
self error: 'Derivative block must implement the method value:' ].
117+
118+
random := Random new.
119+
120+
x := result
121+
ifNil: [ random next ]
122+
ifNotNil: [ result + random next ].
123+
114124
((aBlock value: x)
115-
relativelyEqualsTo: (self defaultDerivativeBlock value: x)
116-
upTo: 1.0e-4)
117-
ifFalse: [self error: 'Supplied derivative is not correct'].
125+
closeTo: (self defaultDerivativeBlock value: x)
126+
precision: 1.0e-4)
127+
ifFalse: [ self error: 'Supplied derivative is not correct' ].
128+
118129
derivativeBlock := aBlock
119130
]
120131

src/Math-Numerical/PMOptimizingBracketFinder.class.st

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ PMOptimizingBracketFinder class >> initialPoints: aSortedCollection function: aF
1212

1313
{ #category : #operation }
1414
PMOptimizingBracketFinder >> computeInitialValues [
15-
[bestPoints size < 2] whileTrue: [self addPointAt: Number random]
15+
| random |
16+
random := Random new.
17+
18+
[ bestPoints size < 2 ] whileTrue: [
19+
self addPointAt: random next ]
1620
]
1721

1822
{ #category : #operation }

src/Math-Quaternion/PMQuaternion.class.st

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ PMQuaternion class >> qr: qr qi: qi qj: qj qk: qk [
5757

5858
{ #category : #'*Math-Quaternion' }
5959
PMQuaternion class >> random [
60-
"Answers a random quaternion with abs at most one."
60+
"Answers a random quaternion with abs at most one."
61+
62+
| random |
63+
random := Random new.
6164

62-
^ (0.5 - Float random)
63-
i: (0.5 - Float random)
64-
j: (0.5 - Float random)
65-
k: (0.5 - Float random).
65+
^ (0.5 - random next)
66+
i: (0.5 - random next)
67+
j: (0.5 - random next)
68+
k: (0.5 - random next).
6669
]
6770

6871
{ #category : #'constants access' }

src/Math-Quaternion/PMQuaternion.extension.st

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,25 @@ PMQuaternion >> productWithVector: aVector [
1616
^aVector collect: [ :each | each * self]
1717
]
1818

19-
{ #category : #'*Math-Quaternion' }
20-
PMQuaternion class >> random [
21-
"Answers a random quaternion with abs at most one."
22-
23-
^ (0.5 - Float random)
24-
i: (0.5 - Float random)
25-
j: (0.5 - Float random)
26-
k: (0.5 - Float random).
27-
]
28-
2919
{ #category : #'*Math-Quaternion' }
3020
PMQuaternion >> random [
3121
"analog to Number>>random. The resulting quaternion will have abs at most that of the receiver"
3222
^ self class random * self.
3323
]
3424

25+
{ #category : #'*Math-Quaternion' }
26+
PMQuaternion class >> random [
27+
"Answers a random quaternion with abs at most one."
28+
29+
| random |
30+
random := Random new.
31+
32+
^ (0.5 - random next)
33+
i: (0.5 - random next)
34+
j: (0.5 - random next)
35+
k: (0.5 - random next).
36+
]
37+
3538
{ #category : #'*Math-Quaternion' }
3639
PMQuaternion >> subtractToPolynomial: aPolynomial [
3740
^aPolynomial addNumber: self negated

src/Math-Tests-Complex/PMComplexTest.class.st

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,9 @@ PMComplexTest >> testRaisedToInteger [
579579

580580
{ #category : #tests }
581581
PMComplexTest >> testRandom [
582-
| c r |
583-
c := Float random + Float random i.
582+
| random c r |
583+
random := Random new.
584+
c := random next + random next i.
584585
r := c random.
585586
self assert: r isComplexNumber.
586587
self assert: r abs < c abs

0 commit comments

Comments
 (0)