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

Warn about using rand() and the seed functions #386

Merged
merged 3 commits into from Sep 2, 2014
Merged

Warn about using rand() and the seed functions #386

merged 3 commits into from Sep 2, 2014

Conversation

ghost
Copy link

@ghost ghost commented Aug 8, 2014

Using $faker with the seed functionality $faker->seed(1337) and PHP function rand() break seed:

<?php
require_once 'Faker-master/src/autoload.php';

$faker = Faker\Factory::create('en_AU');
$faker->seed(1337);

for ($i = 0; $i < 3; $i++) {
    print $faker->numberBetween(1, 10) . PHP_EOL;
    print $faker->realText(rand(10,20)) . PHP_EOL;
}

First run generate

8
I fell.
8
I did:.
8
Morcar,.

Second run generate

8
I fell off.
4
Queen, and.
10
.

Using $faker or mt_rand() is the key to avoid this problem

<?php
require_once 'Faker-master/src/autoload.php';

$faker = Faker\Factory::create('en_AU');
$faker->seed(1337);

for ($i = 0; $i < 3; $i++) {
    print $faker->numberBetween(1, 10) . PHP_EOL;
    //use faker instead of rand()
    print $faker->realText($faker->numberBetween(10,20)) . PHP_EOL;
}

First run

8
Pigeon; 'but.
6
This time there.
6
The Mouse looked.

Second run

8
Pigeon; 'but.
6
This time there.
6
The Mouse looked.

> for ($i = 0; $i < 3; $i++) {
> print $faker->numberBetween(1, 10) . PHP_EOL;
> //BAD
> print $faker->realText(rand(10,20)) . PHP_EOL;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the examples are too long. You should reduce them to two lines:

  • one call to seed()
  • one call to the formatter

without the for loop, which is not necessary.

fzaninotto added a commit that referenced this pull request Sep 2, 2014
Warn about using rand() and the seed functions
@fzaninotto fzaninotto merged commit c6857ca into fzaninotto:master Sep 2, 2014
@fzaninotto
Copy link
Owner

Thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants