From cf4acd1947ceda574d56ef858172dc126f135007 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Fri, 31 May 2019 11:06:33 +0100 Subject: [PATCH 01/19] [issue-105] Removed an obselete test. --- .../PMBernoulliGeneratorTest.class.st | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/Math-Tests-Random/PMBernoulliGeneratorTest.class.st b/src/Math-Tests-Random/PMBernoulliGeneratorTest.class.st index 2ff085cdb..ec209a95a 100644 --- a/src/Math-Tests-Random/PMBernoulliGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMBernoulliGeneratorTest.class.st @@ -7,17 +7,6 @@ Class { #category : #'Math-Tests-Random' } -{ #category : #tests } -PMBernoulliGeneratorTest >> testGenerator [ - | g bern | - g := PMLinearCongruentialRandomGenerator new. - bern := PMBernoulliGenerator new. - self - assert: (bern generator isKindOf: PMBernoulliGenerator defaultGeneratorClass). - bern generator: g. - self assert: (bern generator isKindOf: PMLinearCongruentialRandomGenerator) -] - { #category : #tests } PMBernoulliGeneratorTest >> testNextYieldsOneOrZero [ | gen | From 51678f285c92710c1ba466147fd8a80f5ee6b71c Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Fri, 31 May 2019 11:56:59 +0100 Subject: [PATCH 02/19] [issue-105] Improved the name of the class. The class comment says it is a generator. --- ...t => PMMarsagliaKissRandomGenerator.class.st} | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename src/Math-Random/{PMMarsagliaKissRandom.class.st => PMMarsagliaKissRandomGenerator.class.st} (81%) diff --git a/src/Math-Random/PMMarsagliaKissRandom.class.st b/src/Math-Random/PMMarsagliaKissRandomGenerator.class.st similarity index 81% rename from src/Math-Random/PMMarsagliaKissRandom.class.st rename to src/Math-Random/PMMarsagliaKissRandomGenerator.class.st index cfa6fd571..7c211397b 100644 --- a/src/Math-Random/PMMarsagliaKissRandom.class.st +++ b/src/Math-Random/PMMarsagliaKissRandomGenerator.class.st @@ -10,7 +10,7 @@ Instance Variables " Class { - #name : #PMMarsagliaKissRandom, + #name : #PMMarsagliaKissRandomGenerator, #superclass : #PMRandomGenerator, #instVars : [ 'kernelRand1', @@ -20,20 +20,20 @@ Class { } { #category : #'instance creation' } -PMMarsagliaKissRandom class >> default [ +PMMarsagliaKissRandomGenerator class >> default [ ^self seed: #( 123456789 362436069 521288629 316191069 987654321 458629013 582859209 438195021) ] { #category : #'instance creation' } -PMMarsagliaKissRandom class >> new [ +PMMarsagliaKissRandomGenerator class >> new [ ^ self seed: (PMMarsagliaKissRandomKernel new next: 8) ] { #category : #'stream access' } -PMMarsagliaKissRandom >> next [ +PMMarsagliaKissRandomGenerator >> next [ "Answer a Float in interval [0.0,1.0) with uniform distribution. Note that constant 16rFFFFF800 is computed so as to truncate the 64 bits to Float precision. It is thus ((1 << 32 - 1 << (64 - Float precision) bitAnd: 1 << 32 - 1)) hex" @@ -42,23 +42,23 @@ PMMarsagliaKissRandom >> next [ ] { #category : #'stream access' } -PMMarsagliaKissRandom >> peek [ +PMMarsagliaKissRandomGenerator >> peek [ ^self copy next ] { #category : #copying } -PMMarsagliaKissRandom >> postCopy [ +PMMarsagliaKissRandomGenerator >> postCopy [ kernelRand1 := kernelRand1 copy. kernelRand2 := kernelRand2 copy ] { #category : #accessing } -PMMarsagliaKissRandom >> seed [ +PMMarsagliaKissRandomGenerator >> seed [ ^ kernelRand1 seed , kernelRand2 seed ] { #category : #accessing } -PMMarsagliaKissRandom >> seed: aWordArray [ +PMMarsagliaKissRandomGenerator >> seed: aWordArray [ "Initialize with an Array of eight 32-bits Integer" kernelRand1 := PMMarsagliaKissRandomKernel seed: (aWordArray first: 4). kernelRand2 := PMMarsagliaKissRandomKernel seed: (aWordArray last: 4) From b741754033b752ffc4d2d4885f757fcf5ea8c4b9 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Fri, 31 May 2019 13:32:31 +0100 Subject: [PATCH 03/19] [issue-105] Improved the name of the class. --- .../PMLehmerRandomGenerator.class.st | 2 +- ...LinearCongruentialRandomGenerator.class.st | 2 +- .../PMMarsagliaKissRandomGenerator.class.st | 2 +- .../PMMersenneTwisterRandomGenerator.class.st | 4 +-- ...MParkMillerMinimumRandomGenerator.class.st | 2 +- ...=> PMPseudoRandomNumberGenerator.class.st} | 28 ++++++++--------- .../PMExponentialGeneratorTest.class.st | 2 +- ...MPseudoRandomNumberGeneratorTest.class.st} | 30 +++++++++---------- 8 files changed, 36 insertions(+), 36 deletions(-) rename src/Math-Random/{PMRandomGenerator.class.st => PMPseudoRandomNumberGenerator.class.st} (84%) rename src/Math-Tests-Random/{PMRandomGeneratorTest.class.st => PMPseudoRandomNumberGeneratorTest.class.st} (56%) diff --git a/src/Math-Random/PMLehmerRandomGenerator.class.st b/src/Math-Random/PMLehmerRandomGenerator.class.st index 3659ff66c..3dc1ba7a6 100644 --- a/src/Math-Random/PMLehmerRandomGenerator.class.st +++ b/src/Math-Random/PMLehmerRandomGenerator.class.st @@ -5,7 +5,7 @@ PMLehmerRandomGenerator new next " Class { #name : #PMLehmerRandomGenerator, - #superclass : #PMRandomGenerator, + #superclass : #PMPseudoRandomNumberGenerator, #category : #'Math-Random' } diff --git a/src/Math-Random/PMLinearCongruentialRandomGenerator.class.st b/src/Math-Random/PMLinearCongruentialRandomGenerator.class.st index c8b99c60a..543ad6704 100644 --- a/src/Math-Random/PMLinearCongruentialRandomGenerator.class.st +++ b/src/Math-Random/PMLinearCongruentialRandomGenerator.class.st @@ -5,7 +5,7 @@ PMLinearCongruentialRandomGenerator new next. " Class { #name : #PMLinearCongruentialRandomGenerator, - #superclass : #PMRandomGenerator, + #superclass : #PMPseudoRandomNumberGenerator, #category : #'Math-Random' } diff --git a/src/Math-Random/PMMarsagliaKissRandomGenerator.class.st b/src/Math-Random/PMMarsagliaKissRandomGenerator.class.st index 7c211397b..1660910c9 100644 --- a/src/Math-Random/PMMarsagliaKissRandomGenerator.class.st +++ b/src/Math-Random/PMMarsagliaKissRandomGenerator.class.st @@ -11,7 +11,7 @@ Instance Variables " Class { #name : #PMMarsagliaKissRandomGenerator, - #superclass : #PMRandomGenerator, + #superclass : #PMPseudoRandomNumberGenerator, #instVars : [ 'kernelRand1', 'kernelRand2' diff --git a/src/Math-Random/PMMersenneTwisterRandomGenerator.class.st b/src/Math-Random/PMMersenneTwisterRandomGenerator.class.st index 616aa72fb..dd16ed7f7 100644 --- a/src/Math-Random/PMMersenneTwisterRandomGenerator.class.st +++ b/src/Math-Random/PMMersenneTwisterRandomGenerator.class.st @@ -11,7 +11,7 @@ MersenneTwisterRandom new nextInteger. " Class { #name : #PMMersenneTwisterRandomGenerator, - #superclass : #PMRandomGenerator, + #superclass : #PMPseudoRandomNumberGenerator, #instVars : [ 'states', 'mti' @@ -26,7 +26,7 @@ Class { 'TemperingMaskB', 'TemperingMaskC' ], - #category : 'Math-Random' + #category : #'Math-Random' } { #category : #'class initialization' } diff --git a/src/Math-Random/PMParkMillerMinimumRandomGenerator.class.st b/src/Math-Random/PMParkMillerMinimumRandomGenerator.class.st index b67d468d9..53c0653c0 100644 --- a/src/Math-Random/PMParkMillerMinimumRandomGenerator.class.st +++ b/src/Math-Random/PMParkMillerMinimumRandomGenerator.class.st @@ -5,7 +5,7 @@ PMParkMillerMinimumRandomGenerator new next " Class { #name : #PMParkMillerMinimumRandomGenerator, - #superclass : #PMRandomGenerator, + #superclass : #PMPseudoRandomNumberGenerator, #category : #'Math-Random' } diff --git a/src/Math-Random/PMRandomGenerator.class.st b/src/Math-Random/PMPseudoRandomNumberGenerator.class.st similarity index 84% rename from src/Math-Random/PMRandomGenerator.class.st rename to src/Math-Random/PMPseudoRandomNumberGenerator.class.st index 68c775b64..c6e1c03d2 100644 --- a/src/Math-Random/PMRandomGenerator.class.st +++ b/src/Math-Random/PMPseudoRandomNumberGenerator.class.st @@ -31,16 +31,16 @@ And finally, there's a taxonomy more related with the internal implementation, b " Class { - #name : #PMRandomGenerator, + #name : #PMPseudoRandomNumberGenerator, #superclass : #Object, #instVars : [ 'seed' ], - #category : 'Math-Random' + #category : #'Math-Random' } { #category : #testing } -PMRandomGenerator class >> chiSquare: range repeating: anInteger [ +PMPseudoRandomNumberGenerator class >> chiSquare: range repeating: anInteger [ "Run a 'chi-square' test on a random number generator, over a range of integers given by range, repeating anInteger times. 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)." @@ -64,21 +64,21 @@ PMRandomGenerator class >> chiSquare: range repeating: anInteger [ ] { #category : #'instance creation' } -PMRandomGenerator class >> new [ +PMPseudoRandomNumberGenerator class >> new [ "Answer a new instance of the receiver" ^ (self seed: Time millisecondClockValue) initialize ] { #category : #'instance creation' } -PMRandomGenerator class >> seed: anInteger [ +PMPseudoRandomNumberGenerator class >> seed: anInteger [ "Anwer a new Random stream with the initial seed anInteger." ^ self basicNew seed: anInteger; yourself ] { #category : #testing } -PMRandomGenerator >> atEnd [ +PMPseudoRandomNumberGenerator >> atEnd [ "Answer whether the receiver is at its end - never true for a stream of Random numbers" ^ false @@ -86,7 +86,7 @@ PMRandomGenerator >> atEnd [ ] { #category : #accessing } -PMRandomGenerator >> contents [ +PMPseudoRandomNumberGenerator >> contents [ "Answer all of the objects in the collection accessed by the receiver. Implementation Notes: Random streams are infinite, so there is no implementation possible." @@ -95,7 +95,7 @@ PMRandomGenerator >> contents [ ] { #category : #testing } -PMRandomGenerator >> isReadable [ +PMPseudoRandomNumberGenerator >> isReadable [ "Answer whether the receiver can be read from (i.e. it implements the gettableStream protocol)." ^ true @@ -103,7 +103,7 @@ PMRandomGenerator >> isReadable [ ] { #category : #testing } -PMRandomGenerator >> isWriteable [ +PMPseudoRandomNumberGenerator >> isWriteable [ "Answer whether the receiver can be written to (i.e. it implements the puttableStream protocol)." ^ false @@ -111,14 +111,14 @@ PMRandomGenerator >> isWriteable [ ] { #category : #'stream access' } -PMRandomGenerator >> next [ +PMPseudoRandomNumberGenerator >> next [ "Answer a pseudo-Random number" ^ self subclassResponsibility ] { #category : #'stream access' } -PMRandomGenerator >> next: anInteger [ +PMPseudoRandomNumberGenerator >> next: anInteger [ "Answer a collection of size anInteger of pseudo-random Floats between 0 and 1. " ^ ( 1 to: anInteger ) collect: [ :i | self next ] @@ -126,21 +126,21 @@ PMRandomGenerator >> next: anInteger [ ] { #category : #'stream access' } -PMRandomGenerator >> peek [ +PMPseudoRandomNumberGenerator >> peek [ "Answer a pseudo-Random number generated from the next seed, but do not advance down the stream (i.e. self peek = self peek). " ^ self subclassResponsibility ] { #category : #accessing } -PMRandomGenerator >> seed [ +PMPseudoRandomNumberGenerator >> seed [ "Returns the instance var of the receiver for external manipulation" ^ seed ] { #category : #accessing } -PMRandomGenerator >> seed: anObject [ +PMPseudoRandomNumberGenerator >> seed: anObject [ "Sets the instance var of the receiver with anObject. (No checking is done)" seed := anObject diff --git a/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st b/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st index a951ef334..c39690964 100644 --- a/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st @@ -13,7 +13,7 @@ PMExponentialGeneratorTest >> testGenerator [ eg := PMExponentialGenerator new. self assert: (eg generator isKindOf: PMExponentialGenerator defaultGeneratorClass). - self assert: (eg generator isKindOf: PMRandomGenerator). + self assert: (eg generator isKindOf: PMPseudoRandomNumberGenerator). eg generator: PMMersenneTwisterRandomGenerator new. self assert: (eg generator isKindOf: PMMersenneTwisterRandomGenerator). self assert: (eg next isKindOf: Number) diff --git a/src/Math-Tests-Random/PMRandomGeneratorTest.class.st b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st similarity index 56% rename from src/Math-Tests-Random/PMRandomGeneratorTest.class.st rename to src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st index ca33218ce..946067dfd 100644 --- a/src/Math-Tests-Random/PMRandomGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st @@ -1,12 +1,12 @@ Class { - #name : #PMRandomGeneratorTest, + #name : #PMPseudoRandomNumberGeneratorTest, #superclass : #TestCase, #category : #'Math-Tests-Random' } { #category : #tests } -PMRandomGeneratorTest >> testGeneratorStreamDoesntRespondToContents [ - PMRandomGenerator subclasses +PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamDoesntRespondToContents [ + PMPseudoRandomNumberGenerator subclasses do: [ :cls | | gen | gen := cls new. @@ -14,8 +14,8 @@ PMRandomGeneratorTest >> testGeneratorStreamDoesntRespondToContents [ ] { #category : #tests } -PMRandomGeneratorTest >> testGeneratorStreamIsReadOnly [ - PMRandomGenerator subclasses +PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamIsReadOnly [ + PMPseudoRandomNumberGenerator subclasses do: [ :cls | | gen | gen := cls new. @@ -24,16 +24,16 @@ PMRandomGeneratorTest >> testGeneratorStreamIsReadOnly [ ] { #category : #tests } -PMRandomGeneratorTest >> testGeneratorStreamNeverEnds [ - PMRandomGenerator subclasses do: +PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamNeverEnds [ + PMPseudoRandomNumberGenerator subclasses do: [:cls | | gen | gen := cls new. self shouldnt: [gen atEnd]]. ] { #category : #tests } -PMRandomGeneratorTest >> testGeneratorStreamPeekIsNext [ - PMRandomGenerator subclasses +PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamPeekIsNext [ + PMPseudoRandomNumberGenerator subclasses do: [ :cls | | gen value | gen := cls new. @@ -43,8 +43,8 @@ PMRandomGeneratorTest >> testGeneratorStreamPeekIsNext [ ] { #category : #tests } -PMRandomGeneratorTest >> testGeneratorStreamProducesNumbers [ - PMRandomGenerator subclasses +PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesNumbers [ + PMPseudoRandomNumberGenerator subclasses do: [ :cls | | gen | gen := cls new. @@ -53,8 +53,8 @@ PMRandomGeneratorTest >> testGeneratorStreamProducesNumbers [ ] { #category : #tests } -PMRandomGeneratorTest >> testGeneratorStreamProducesRandomNumbers [ - PMRandomGenerator subclasses do: +PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesRandomNumbers [ + PMPseudoRandomNumberGenerator subclasses do: [:cls | | gen value | gen := cls new. value := gen next. @@ -62,8 +62,8 @@ PMRandomGeneratorTest >> testGeneratorStreamProducesRandomNumbers [ ] { #category : #tests } -PMRandomGeneratorTest >> testNextGivesArrayOfNumbers [ - PMRandomGenerator subclasses +PMPseudoRandomNumberGeneratorTest >> testNextGivesArrayOfNumbers [ + PMPseudoRandomNumberGenerator subclasses do: [ :cls | | gen samples | gen := cls new. From d978dc16308b5d1b7589bd1cf2e2c6cf9592eb8b Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Fri, 31 May 2019 13:38:23 +0100 Subject: [PATCH 04/19] [issue-105] Extracted duplicate code to an intention-revealing method. --- ...PMPseudoRandomNumberGeneratorTest.class.st | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st index 946067dfd..fab109770 100644 --- a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st @@ -4,9 +4,14 @@ Class { #category : #'Math-Tests-Random' } +{ #category : #private } +PMPseudoRandomNumberGeneratorTest >> pseudoRandomNumberGenerators [ + ^ PMPseudoRandomNumberGenerator subclasses +] + { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamDoesntRespondToContents [ - PMPseudoRandomNumberGenerator subclasses + self pseudoRandomNumberGenerators do: [ :cls | | gen | gen := cls new. @@ -15,7 +20,7 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamDoesntRespondToContents { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamIsReadOnly [ - PMPseudoRandomNumberGenerator subclasses + self pseudoRandomNumberGenerators do: [ :cls | | gen | gen := cls new. @@ -25,15 +30,16 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamIsReadOnly [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamNeverEnds [ - PMPseudoRandomNumberGenerator subclasses do: - [:cls | | gen | - gen := cls new. - self shouldnt: [gen atEnd]]. + self pseudoRandomNumberGenerators + do: [ :cls | + | gen | + gen := cls new. + self shouldnt: [ gen atEnd ] ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamPeekIsNext [ - PMPseudoRandomNumberGenerator subclasses + self pseudoRandomNumberGenerators do: [ :cls | | gen value | gen := cls new. @@ -44,7 +50,7 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamPeekIsNext [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesNumbers [ - PMPseudoRandomNumberGenerator subclasses + self pseudoRandomNumberGenerators do: [ :cls | | gen | gen := cls new. @@ -54,16 +60,17 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesNumbers [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesRandomNumbers [ - PMPseudoRandomNumberGenerator subclasses do: - [:cls | | gen value | - gen := cls new. - value := gen next. - self shouldnt: [gen next = value]]. + self pseudoRandomNumberGenerators + do: [ :cls | + | gen value | + gen := cls new. + value := gen next. + self shouldnt: [ gen next = value ] ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testNextGivesArrayOfNumbers [ - PMPseudoRandomNumberGenerator subclasses + self pseudoRandomNumberGenerators do: [ :cls | | gen samples | gen := cls new. From 457538fc299c361878d2b2d719c4d4d525e86821 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Fri, 31 May 2019 19:27:40 +0100 Subject: [PATCH 05/19] [issue-105] Extracted duplicate code to a helper method. --- ...PMPseudoRandomNumberGeneratorTest.class.st | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st index fab109770..bae87039a 100644 --- a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st @@ -4,6 +4,18 @@ Class { #category : #'Math-Tests-Random' } +{ #category : #private } +PMPseudoRandomNumberGeneratorTest >> pseudoRNG: aBlock [ + "Instanciates a pseudo random number generator + to yield to the block" + + self pseudoRandomNumberGenerators + do: [ :typeOfGenerator | + | generator | + generator := typeOfGenerator new. + aBlock value: generator ] +] + { #category : #private } PMPseudoRandomNumberGeneratorTest >> pseudoRandomNumberGenerators [ ^ PMPseudoRandomNumberGenerator subclasses @@ -11,38 +23,27 @@ PMPseudoRandomNumberGeneratorTest >> pseudoRandomNumberGenerators [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamDoesntRespondToContents [ - self pseudoRandomNumberGenerators - do: [ :cls | - | gen | - gen := cls new. - self should: [ gen contents ] raise: Error ] + self pseudoRNG: [ :gen | self should: [ gen contents ] raise: Error ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamIsReadOnly [ - self pseudoRandomNumberGenerators - do: [ :cls | - | gen | - gen := cls new. + self + pseudoRNG: [ :gen | self shouldnt: [ gen isWriteable ]. self assert: gen isReadable ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamNeverEnds [ - self pseudoRandomNumberGenerators - do: [ :cls | - | gen | - gen := cls new. - self shouldnt: [ gen atEnd ] ] + self pseudoRNG: [ :gen | self shouldnt: [ gen atEnd ] ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamPeekIsNext [ - self pseudoRandomNumberGenerators - do: [ :cls | - | gen value | - gen := cls new. + self + pseudoRNG: [ :gen | + | value | value := gen peek. self assert: gen peek equals: value. self assert: gen next equals: value ] @@ -50,30 +51,26 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamPeekIsNext [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesNumbers [ - self pseudoRandomNumberGenerators - do: [ :cls | - | gen | - gen := cls new. + self + pseudoRNG: [ :gen | self assert: gen next isNumber. self assert: gen next isFloat ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesRandomNumbers [ - self pseudoRandomNumberGenerators - do: [ :cls | - | gen value | - gen := cls new. + self + pseudoRNG: [ :gen | + | value | value := gen next. self shouldnt: [ gen next = value ] ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testNextGivesArrayOfNumbers [ - self pseudoRandomNumberGenerators - do: [ :cls | - | gen samples | - gen := cls new. + self + pseudoRNG: [ :gen | + | samples | samples := gen next: 10. self assert: samples size equals: 10. self assert: (samples at: 1) isFloat. From 2099f2675d0c75e5aa1e26c7affae925b11a4c5f Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Fri, 31 May 2019 19:31:26 +0100 Subject: [PATCH 06/19] [issue-105] Inlined the method as it's only used in one place. --- .../PMPseudoRandomNumberGeneratorTest.class.st | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st index bae87039a..06d6cd94a 100644 --- a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st @@ -9,18 +9,13 @@ PMPseudoRandomNumberGeneratorTest >> pseudoRNG: aBlock [ "Instanciates a pseudo random number generator to yield to the block" - self pseudoRandomNumberGenerators + PMPseudoRandomNumberGenerator subclasses do: [ :typeOfGenerator | | generator | generator := typeOfGenerator new. aBlock value: generator ] ] -{ #category : #private } -PMPseudoRandomNumberGeneratorTest >> pseudoRandomNumberGenerators [ - ^ PMPseudoRandomNumberGenerator subclasses -] - { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamDoesntRespondToContents [ self pseudoRNG: [ :gen | self should: [ gen contents ] raise: Error ] From b988f04f9e784e2444190c573db4d3c8cf4419e1 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Fri, 31 May 2019 21:17:01 +0100 Subject: [PATCH 07/19] [issue-105] Improved the name of the method. --- .../PMPseudoRandomNumberGeneratorTest.class.st | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st index 06d6cd94a..7145cc0d5 100644 --- a/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPseudoRandomNumberGeneratorTest.class.st @@ -5,7 +5,7 @@ Class { } { #category : #private } -PMPseudoRandomNumberGeneratorTest >> pseudoRNG: aBlock [ +PMPseudoRandomNumberGeneratorTest >> pseudoRandomNumberGeneratorsDo: aBlock [ "Instanciates a pseudo random number generator to yield to the block" @@ -18,26 +18,28 @@ PMPseudoRandomNumberGeneratorTest >> pseudoRNG: aBlock [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamDoesntRespondToContents [ - self pseudoRNG: [ :gen | self should: [ gen contents ] raise: Error ] + self + pseudoRandomNumberGeneratorsDo: [ :gen | self should: [ gen contents ] raise: Error ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamIsReadOnly [ self - pseudoRNG: [ :gen | + pseudoRandomNumberGeneratorsDo: [ :gen | self shouldnt: [ gen isWriteable ]. self assert: gen isReadable ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamNeverEnds [ - self pseudoRNG: [ :gen | self shouldnt: [ gen atEnd ] ] + self + pseudoRandomNumberGeneratorsDo: [ :gen | self shouldnt: [ gen atEnd ] ] ] { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamPeekIsNext [ self - pseudoRNG: [ :gen | + pseudoRandomNumberGeneratorsDo: [ :gen | | value | value := gen peek. self assert: gen peek equals: value. @@ -47,7 +49,7 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamPeekIsNext [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesNumbers [ self - pseudoRNG: [ :gen | + pseudoRandomNumberGeneratorsDo: [ :gen | self assert: gen next isNumber. self assert: gen next isFloat ] ] @@ -55,7 +57,7 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesNumbers [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesRandomNumbers [ self - pseudoRNG: [ :gen | + pseudoRandomNumberGeneratorsDo: [ :gen | | value | value := gen next. self shouldnt: [ gen next = value ] ] @@ -64,7 +66,7 @@ PMPseudoRandomNumberGeneratorTest >> testGeneratorStreamProducesRandomNumbers [ { #category : #tests } PMPseudoRandomNumberGeneratorTest >> testNextGivesArrayOfNumbers [ self - pseudoRNG: [ :gen | + pseudoRandomNumberGeneratorsDo: [ :gen | | samples | samples := gen next: 10. self assert: samples size equals: 10. From 6539d562f6d6a3dd29f70aefce6d4fddc47127a0 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 2 Jun 2019 20:10:42 +0100 Subject: [PATCH 08/19] [issue-105] Corrected code style. --- src/Math-Tests-Random/PMBinomialGeneratorTest.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st index 42196e9af..61c9378c8 100644 --- a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st @@ -17,7 +17,7 @@ PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [ probabilityOfSuccess: probabilityOfSuccess. nums := OrderedCollection new. - (1 to: numberOfTrials) do: [ :ea | nums add: gen next ]. + 1 to: numberOfTrials do: [ :ea | nums add: gen next ]. mean := numberOfTrials * probabilityOfSuccess. self assert: nums min > (mean * (1 - 0.2)). From 96a4e6a8fb4ae249cb0e9c9b67727bdc7ac90516 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 2 Jun 2019 20:16:56 +0100 Subject: [PATCH 09/19] [issue-105] Improved the name of the method. --- src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st b/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st index 1b1cb51d8..618bfec53 100644 --- a/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st @@ -10,8 +10,8 @@ PMLaplaceGeneratorTest >> classToTest [ ] { #category : #accessing } -PMLaplaceGeneratorTest >> testPeekAlwaysReplyTheSameValue [ +PMLaplaceGeneratorTest >> testPeekIsIdempotent [ | g | - g := self classToTest shape: 0.5 scale: 0.3. + g := PMLaplaceGenerator shape: 0.5 scale: 0.3. self assert: g peek equals: g peek ] From bfa974670e1ff215f74cf2d83e744a5dbf52c673 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 2 Jun 2019 20:18:14 +0100 Subject: [PATCH 10/19] [issue-105] Removed obselete code. --- src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st b/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st index 618bfec53..cc7edad8d 100644 --- a/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st @@ -4,11 +4,6 @@ Class { #category : #'Math-Tests-Random' } -{ #category : #'as yet unclassified' } -PMLaplaceGeneratorTest >> classToTest [ - ^ PMLaplaceGenerator -] - { #category : #accessing } PMLaplaceGeneratorTest >> testPeekIsIdempotent [ | g | From 56926adf32e98d213a687b5d523480af98c80bed Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Sun, 2 Jun 2019 20:19:52 +0100 Subject: [PATCH 11/19] [issue-105] Corrected the category. --- src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st b/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st index cc7edad8d..2573b654e 100644 --- a/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMLaplaceGeneratorTest.class.st @@ -4,7 +4,7 @@ Class { #category : #'Math-Tests-Random' } -{ #category : #accessing } +{ #category : #tests } PMLaplaceGeneratorTest >> testPeekIsIdempotent [ | g | g := PMLaplaceGenerator shape: 0.5 scale: 0.3. From b0ded6e1256b9e2f4ca314d4d7f09d713df4e56c Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 14:34:30 +0100 Subject: [PATCH 12/19] [issue-105] Improved the names of the methods. --- src/Math-Tests-Random/PMExponentialGeneratorTest.class.st | 2 +- src/Math-Tests-Random/PMLehmerRandomTest.class.st | 8 ++++---- .../PMLinearCongruentialRandomTest.class.st | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st b/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st index c39690964..6fdbfeb12 100644 --- a/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st @@ -20,7 +20,7 @@ PMExponentialGeneratorTest >> testGenerator [ ] { #category : #tests } -PMExponentialGeneratorTest >> testPeekAlwaysAnswersTheSame [ +PMExponentialGeneratorTest >> testPeekIsIdempotent [ | eg | eg := PMExponentialGenerator new. self assert: eg peek equals: eg peek. diff --git a/src/Math-Tests-Random/PMLehmerRandomTest.class.st b/src/Math-Tests-Random/PMLehmerRandomTest.class.st index 21f3ce4a4..55740df49 100644 --- a/src/Math-Tests-Random/PMLehmerRandomTest.class.st +++ b/src/Math-Tests-Random/PMLehmerRandomTest.class.st @@ -14,15 +14,15 @@ PMLehmerRandomTest >> testNextBetweenZeroAndOne [ ] { #category : #tests } -PMLehmerRandomTest >> testPeekAlwaysReplyTheSameValue [ +PMLehmerRandomTest >> testPeekAnswersSameAsNext [ | g | g := PMLehmerRandomGenerator new. - self assert: g peek equals: g peek + self assert: g peek equals: g next ] { #category : #tests } -PMLehmerRandomTest >> testPeekAnswersSameAsNext [ +PMLehmerRandomTest >> testPeekIsIdempotent [ | g | g := PMLehmerRandomGenerator new. - self assert: g peek equals: g next + self assert: g peek equals: g peek ] diff --git a/src/Math-Tests-Random/PMLinearCongruentialRandomTest.class.st b/src/Math-Tests-Random/PMLinearCongruentialRandomTest.class.st index d1dafadb4..08ae003cb 100644 --- a/src/Math-Tests-Random/PMLinearCongruentialRandomTest.class.st +++ b/src/Math-Tests-Random/PMLinearCongruentialRandomTest.class.st @@ -14,15 +14,15 @@ PMLinearCongruentialRandomTest >> testNextBetweenZeroAndOne [ ] { #category : #tests } -PMLinearCongruentialRandomTest >> testPeekAlwaysReplyTheSameValue [ +PMLinearCongruentialRandomTest >> testPeekAnswersSameAsNext [ | g | g := PMLinearCongruentialRandomGenerator new. - self assert: g peek equals: g peek + self assert: g peek equals: g next ] { #category : #tests } -PMLinearCongruentialRandomTest >> testPeekAnswersSameAsNext [ +PMLinearCongruentialRandomTest >> testPeekIsIdempotent [ | g | g := PMLinearCongruentialRandomGenerator new. - self assert: g peek equals: g next + self assert: g peek equals: g peek ] From fe8b9ea501044de1084c09414b2a3df2bffd9f35 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 15:51:39 +0100 Subject: [PATCH 13/19] [issue-105] Improved the names of the method. --- src/Math-Tests-Random/PMNumberGeneratorTest.class.st | 2 +- src/Math-Tests-Random/PMPoissonGeneratorTest.class.st | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Math-Tests-Random/PMNumberGeneratorTest.class.st b/src/Math-Tests-Random/PMNumberGeneratorTest.class.st index cd37c918c..82c45070b 100644 --- a/src/Math-Tests-Random/PMNumberGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMNumberGeneratorTest.class.st @@ -5,7 +5,7 @@ Class { } { #category : #tests } -PMNumberGeneratorTest >> testPeekAnswersSame [ +PMNumberGeneratorTest >> testPeekIsIdempotent [ "every subclass should implement some basic behavior." PMNumberGenerator subclasses diff --git a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st index 7f1607863..c3bf7e140 100644 --- a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st @@ -5,11 +5,10 @@ Class { } { #category : #tests } -PMPoissonGeneratorTest >> testPeekReturnsSameAsNext [ - | poisson random | +PMPoissonGeneratorTest >> testPeekIsIdempotent [ + | poisson | poisson := PMPoissonGenerator lambda: 0.1. - random := poisson peek. - self assert: random equals: poisson next + self assert: poisson peek equals: poisson peek ] { #category : #tests } From 6fce1ea33e08b5394797504465c1c65652d90114 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 15:55:30 +0100 Subject: [PATCH 14/19] [issue-105] Removed obselete test. --- src/Math-Tests-Random/PMPoissonGeneratorTest.class.st | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st index c3bf7e140..ec6d010a8 100644 --- a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st @@ -11,14 +11,6 @@ PMPoissonGeneratorTest >> testPeekIsIdempotent [ self assert: poisson peek equals: poisson peek ] -{ #category : #tests } -PMPoissonGeneratorTest >> testPeekReturnsSameTwice [ - | poisson random | - poisson := PMPoissonGenerator lambda: 0.1. - random := poisson peek. - self assert: random equals: poisson peek -] - { #category : #tests } PMPoissonGeneratorTest >> testPeekWorksAfterSampling [ | poisson random | From 5ba526ad4ccba95e42355a4e18939653b58e90c4 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 16:00:21 +0100 Subject: [PATCH 15/19] [issue-105] Brought back a test deleted accidentally. --- src/Math-Tests-Random/PMPoissonGeneratorTest.class.st | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st index ec6d010a8..4bac5e47e 100644 --- a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st @@ -11,6 +11,13 @@ PMPoissonGeneratorTest >> testPeekIsIdempotent [ self assert: poisson peek equals: poisson peek ] +{ #category : #tests } +PMPoissonGeneratorTest >> testPeekReturnsSameAsNext [ + | poisson | + poisson := PMPoissonGenerator lambda: 0.1. + self assert: poisson peek equals: poisson next +] + { #category : #tests } PMPoissonGeneratorTest >> testPeekWorksAfterSampling [ | poisson random | From e970b168783f2215de8889d28e45548750d3f0f4 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 17:39:15 +0100 Subject: [PATCH 16/19] [issue-105] Made similar code even more similar. --- .../PMBinomialGeneratorTest.class.st | 12 ++++++------ .../PMExponentialGeneratorTest.class.st | 11 ++++++----- .../PMPoissonGeneratorTest.class.st | 14 ++++++++------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st index 61c9378c8..94ad6cf6d 100644 --- a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st @@ -9,20 +9,20 @@ PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [ "Its purpose is to verify correct convergence of the binomial distribution, should be Normal (np, np(1-p))" - | gen nums mean probabilityOfSuccess numberOfTrials | + | gen sample mean probabilityOfSuccess numberOfTrials | probabilityOfSuccess := (Random seed: 0.0) next sqrt. numberOfTrials := 1000. gen := PMBinomialGenerator numberOfTrials: numberOfTrials probabilityOfSuccess: probabilityOfSuccess. - nums := OrderedCollection new. - 1 to: numberOfTrials do: [ :ea | nums add: gen next ]. + sample := OrderedCollection new. + 1 to: numberOfTrials do: [ :ea | sample add: gen next ]. mean := numberOfTrials * probabilityOfSuccess. - self assert: nums min > (mean * (1 - 0.2)). - self assert: nums max < (mean * (1 + 0.2)). - self assert: (nums average - mean) abs < 5 + self assert: sample min > (mean * (1 - 0.2)). + self assert: sample max < (mean * (1 + 0.2)). + self assert: (sample average - mean) abs < 5 ] { #category : #tests } diff --git a/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st b/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st index 6fdbfeb12..c18e39354 100644 --- a/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMExponentialGeneratorTest.class.st @@ -31,10 +31,11 @@ PMExponentialGeneratorTest >> testPeekIsIdempotent [ PMExponentialGeneratorTest >> testSampleMeanConvergesToDistributionMean [ "testing a random sample seems suspect. We use a 5% interval here" - | eg arr | - eg := PMExponentialGenerator mean: 10. - arr := Array new: 10000. - arr := arr collect: [ :each | eg next ]. + | gen sample | + gen := PMExponentialGenerator mean: 10. + sample := Array new: 10000. + sample := sample collect: [ :each | gen next ]. + self - assert: (arr average between: eg mean * 0.95 and: eg mean * 1.05) + assert: (sample average between: gen mean * 0.95 and: gen mean * 1.05) ] diff --git a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st index 4bac5e47e..535a48d70 100644 --- a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st @@ -29,16 +29,18 @@ PMPoissonGeneratorTest >> testPeekWorksAfterSampling [ ] { #category : #tests } -PMPoissonGeneratorTest >> testSampleAverageConvergesToLambda [ +PMPoissonGeneratorTest >> testSampleMeanConvergesToLambda [ "law of large numbers" "made non-random, old random version in comments" - | poisson samples | - samples := OrderedCollection new. + | poisson sample | poisson := PMPoissonGenerator lambda: 5. "((1000 atRandom: Random new) -1)" poisson generator seed: 42. "added for non-randomness" - 1000 timesRepeat: [ samples add: poisson next ]. - self assert: samples average >= (poisson lambda * 0.8). - self assert: samples average <= (poisson lambda * 1.2) + + sample := OrderedCollection new. + 1000 timesRepeat: [ sample add: poisson next ]. + + self assert: sample average >= (poisson lambda * 0.8). + self assert: sample average <= (poisson lambda * 1.2) ] From c07a279be2fee2e7fd44fb1f814ad02992eab559 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 17:58:55 +0100 Subject: [PATCH 17/19] [issue-105] Moved the code to a better place, an intention-revealing method. --- src/Math-Random/PMBinomialGenerator.class.st | 5 +++++ src/Math-Tests-Random/PMBinomialGeneratorTest.class.st | 9 ++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Math-Random/PMBinomialGenerator.class.st b/src/Math-Random/PMBinomialGenerator.class.st index 44d4ce551..fa3537ed3 100644 --- a/src/Math-Random/PMBinomialGenerator.class.st +++ b/src/Math-Random/PMBinomialGenerator.class.st @@ -21,6 +21,11 @@ PMBinomialGenerator class >> numberOfTrials: numberOfTrials probabilityOfSuccess yourself ] +{ #category : #accessing } +PMBinomialGenerator >> expectedValue [ + ^ numberOfTrials * probability . +] + { #category : #initialization } PMBinomialGenerator >> initialize [ self generator: PMParkMillerMinimumRandomGenerator new. diff --git a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st index 94ad6cf6d..77bb727d6 100644 --- a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st @@ -9,7 +9,7 @@ PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [ "Its purpose is to verify correct convergence of the binomial distribution, should be Normal (np, np(1-p))" - | gen sample mean probabilityOfSuccess numberOfTrials | + | gen sample probabilityOfSuccess numberOfTrials | probabilityOfSuccess := (Random seed: 0.0) next sqrt. numberOfTrials := 1000. gen := PMBinomialGenerator @@ -19,10 +19,9 @@ PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [ sample := OrderedCollection new. 1 to: numberOfTrials do: [ :ea | sample add: gen next ]. - mean := numberOfTrials * probabilityOfSuccess. - self assert: sample min > (mean * (1 - 0.2)). - self assert: sample max < (mean * (1 + 0.2)). - self assert: (sample average - mean) abs < 5 + self assert: sample min > (gen expectedValue * (1 - 0.2)). + self assert: sample max < (gen expectedValue * (1 + 0.2)). + self assert: (sample average - gen expectedValue) abs < 5 ] { #category : #tests } From 10ad8db2dd7e09fce5547febc1310aafda5318c5 Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 18:03:10 +0100 Subject: [PATCH 18/19] [issue-105] Made the code consistent with that of the other tests. This reads a little better now. --- src/Math-Tests-Random/PMBinomialGeneratorTest.class.st | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st index 77bb727d6..5facad2de 100644 --- a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st @@ -17,7 +17,7 @@ PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [ probabilityOfSuccess: probabilityOfSuccess. sample := OrderedCollection new. - 1 to: numberOfTrials do: [ :ea | sample add: gen next ]. + numberOfTrials timesRepeat: [ sample add: gen next ]. self assert: sample min > (gen expectedValue * (1 - 0.2)). self assert: sample max < (gen expectedValue * (1 + 0.2)). From d40f8e4e24c358bb3403365a15dc99fa736602aa Mon Sep 17 00:00:00 2001 From: Hemal Varambhia Date: Mon, 3 Jun 2019 18:11:53 +0100 Subject: [PATCH 19/19] [issue-105] Made similar code look the same, improved the name of a test method by using domain language. --- .../PMBinomialGeneratorTest.class.st | 40 +++++++++---------- .../PMPoissonGeneratorTest.class.st | 12 +++--- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st index 5facad2de..0b5adaf46 100644 --- a/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMBinomialGeneratorTest.class.st @@ -4,26 +4,6 @@ Class { #category : #'Math-Tests-Random' } -{ #category : #tests } -PMBinomialGeneratorTest >> testBinomialGeneratorConvergesToMean [ - "Its purpose is to verify correct convergence of the binomial distribution, - should be Normal (np, np(1-p))" - - | gen sample probabilityOfSuccess numberOfTrials | - probabilityOfSuccess := (Random seed: 0.0) next sqrt. - numberOfTrials := 1000. - gen := PMBinomialGenerator - numberOfTrials: numberOfTrials - probabilityOfSuccess: probabilityOfSuccess. - - sample := OrderedCollection new. - numberOfTrials timesRepeat: [ sample add: gen next ]. - - self assert: sample min > (gen expectedValue * (1 - 0.2)). - self assert: sample max < (gen expectedValue * (1 + 0.2)). - self assert: (sample average - gen expectedValue) abs < 5 -] - { #category : #tests } PMBinomialGeneratorTest >> testBinomialGeneratorWithSuccessProbabilityOfOneAlwaysReturnNumberOfTrials [ | g numberOfTrials | @@ -55,3 +35,23 @@ PMBinomialGeneratorTest >> testBinomialGeneratorWithSuccessProbabilityOfZeroAlwa g generator: PMMersenneTwisterRandomGenerator new. self assert: g next equals: 0 ] + +{ #category : #tests } +PMBinomialGeneratorTest >> testSampleMeanConvergesToExpectedValue [ + "Its purpose is to verify correct convergence of the binomial distribution, + should be Normal (np, np(1-p))" + + | gen sample probabilityOfSuccess numberOfTrials | + probabilityOfSuccess := (Random seed: 0.0) next sqrt. + numberOfTrials := 1000. + gen := PMBinomialGenerator + numberOfTrials: numberOfTrials + probabilityOfSuccess: probabilityOfSuccess. + + sample := OrderedCollection new. + numberOfTrials timesRepeat: [ sample add: gen next ]. + + self assert: sample min > (gen expectedValue * (1 - 0.2)). + self assert: sample max < (gen expectedValue * (1 + 0.2)). + self assert: (sample average - gen expectedValue) abs < 5 +] diff --git a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st index 535a48d70..c8e399e1d 100644 --- a/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st +++ b/src/Math-Tests-Random/PMPoissonGeneratorTest.class.st @@ -34,13 +34,13 @@ PMPoissonGeneratorTest >> testSampleMeanConvergesToLambda [ "made non-random, old random version in comments" - | poisson sample | - poisson := PMPoissonGenerator lambda: 5. "((1000 atRandom: Random new) -1)" - poisson generator seed: 42. "added for non-randomness" + | gen sample | + gen := PMPoissonGenerator lambda: 5. "((1000 atRandom: Random new) -1)" + gen generator seed: 42. "added for non-randomness" sample := OrderedCollection new. - 1000 timesRepeat: [ sample add: poisson next ]. + 1000 timesRepeat: [ sample add: gen next ]. - self assert: sample average >= (poisson lambda * 0.8). - self assert: sample average <= (poisson lambda * 1.2) + self assert: sample average >= (gen lambda * 0.8). + self assert: sample average <= (gen lambda * 1.2) ]