Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Fix mtrand seed #543

Merged
merged 2 commits into from
Mar 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
15 changes: 15 additions & 0 deletions test/Faker/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down