Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use biasedNumberBetween faker? [closed]

Tags:

php

faker

I am trying to get a number between 1 to 6 with more chances to be close to 1.

i have tried this:

<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>

What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.

like image 449
Gabriel Eduardo Bejarano Rojas Avatar asked Nov 01 '25 01:11

Gabriel Eduardo Bejarano Rojas


1 Answers

Something like this ?

<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
    $arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}

echo weightedRand(1,6, 3);
?>

numbers below 4 will now be more likely than numbers above :)

like image 70
MrK Avatar answered Nov 02 '25 21:11

MrK