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

Commit

Permalink
Merge pull request #543 from schmengler/fix-mtrand-seed
Browse files Browse the repository at this point in the history
Fix mtrand seed
  • Loading branch information
fzaninotto committed Mar 30, 2015
2 parents 65a9d28 + 377bbd6 commit 6b1d646
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,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

0 comments on commit 6b1d646

Please sign in to comment.