From 8cc3bd03384425688fad4333b4de531a8fce9cc0 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Thu, 19 Mar 2015 08:38:17 +0100 Subject: [PATCH 1/2] Make calling seed() without parameters seeding the RNG with random seed. This fixes issue #538 --- src/Faker/Generator.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index b3c7bc4a8a..1e8e1fabfa 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -145,7 +145,11 @@ public function getProviders() public function seed($seed = null) { - mt_srand($seed); + if ($seed === null) { + mt_srand(); + } else { + mt_srand($seed); + } } public function format($formatter, $arguments = array()) From 377bbd6076dedaa6e60d0cef1ddb6dea0f2e2cca Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Thu, 19 Mar 2015 08:48:58 +0100 Subject: [PATCH 2/2] Add test case for #538 --- test/Faker/GeneratorTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/Faker/GeneratorTest.php b/test/Faker/GeneratorTest.php index 147d9ddd18..9fea4f867a 100644 --- a/test/Faker/GeneratorTest.php +++ b/test/Faker/GeneratorTest.php @@ -105,6 +105,21 @@ public function testMagicCallCallsFormatWithArguments() $this->assertEquals('bazfoo', $generator->fooFormatterWithArguments('foo')); } + public function testSeed() + { + $generator = new Generator; + + $generator->seed(0); + $mtRandWithSeedZero = mt_rand(); + $generator->seed(0); + $this->assertEquals($mtRandWithSeedZero, mt_rand(), 'seed(0) should be deterministic.'); + + $generator->seed(); + $mtRandWithoutSeed = mt_rand(); + $this->assertNotEquals($mtRandWithSeedZero, $mtRandWithoutSeed, 'seed() should be different than seed(0)'); + $generator->seed(); + $this->assertNotEquals($mtRandWithoutSeed, mt_rand(), 'seed() should not be deterministic.'); + } } class FooProvider