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

Adding ability to generate a random normally distributed number #1817

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
randomNumber($nbDigits = NULL, $strict = false) // 79907610
randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932
numberBetween($min = 1000, $max = 9000) // 8567
normallyDistributedNumber($avg = 0, $sd = 1) // 2
randomLetter // 'b'
// returns randomly ordered subsequence of a provided array
randomElements($array = array ('a','b','c'), $count = 1) // array('c')
Expand Down
1 change: 1 addition & 0 deletions src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
* @method int randomNumber($nbDigits = null, $strict = false)
* @method int|string|null randomKey(array $array = array())
* @method int numberBetween($min = 0, $max = 2147483647)
* @method int normallyDistributedNumber($avg = 0, $sd = 1)
* @method float randomFloat($nbMaxDecimals = null, $min = 0, $max = null)
* @method mixed randomElement(array $array = array('a', 'b', 'c'))
* @method array randomElements(array $array = array('a', 'b', 'c'), $count = 1, $allowDuplicates = false)
Expand Down
41 changes: 41 additions & 0 deletions src/Faker/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,47 @@ public static function numberBetween($int1 = 0, $int2 = 2147483647)
$max = $int1 < $int2 ? $int2 : $int1;
return mt_rand($min, $max);
}

/**
* Returns a random number normally distributed about the mean and standard deviation
*
* @param integer $avg
* @param integer $sd
*
* @return integer
*/
public static function normallyDistributedNumber($avg = 0, $sd = 1)
{
$random_arrray = [];
$random_sign = [1, -1];

$sd2 = $sd*2;
$sd3 = $sd*3;
$sd4 = $sd*4;


for ($i=0; $i < 683; $i++) {
$random_number = $avg + (static::numberBetween(0, $sd) * $random_sign[array_rand($random_sign)]);
array_push($random_arrray, $random_number);
}

for ($i=0; $i < 271; $i++) {
$random_number = $avg + (static::numberBetween($sd, $sd2) * $random_sign[array_rand($random_sign)]);
array_push($random_arrray, $random_number);
}

for ($i=0; $i < 43; $i++) {
$random_number = $avg + (static::numberBetween($sd2, $sd3) * $random_sign[array_rand($random_sign)]);
array_push($random_arrray, $random_number);
}

for ($i=0; $i < 3; $i++) {
$random_number = $avg + (static::numberBetween($sd3, $sd4) * $random_sign[array_rand($random_sign)]);
array_push($random_arrray, $random_number);
}

return $random_arrray[array_rand($random_arrray)];
}

/**
* Returns the passed value
Expand Down