Home > Back-end >  PHP random numbers frequency of occurrence
PHP random numbers frequency of occurrence

Time:01-16

In PHP I want to generate random numbers from 1 to 10 in a loop.

So for example:

$factor="1"; // this will be changed in another area

for ($i=0;$i<10;$i  ) {
if ($factor=="1") {$output = rand(1,10);}
else if ($factor=="2") {$output = rand(1,10);}
else {$output = rand(1,10);}

 } 

Now to explain this - In result I want to receive 10 random numbers, but when $factor = "2", in that case I want to receive numbers from 6 to 10 more frequently as lower numbers.

It means, from 10 numbers I need to have 80% higher random numbers (it means larger than 5) and in 20% lower numbers (5 or lower).

E.g. 1,2,6,7,8,9,7,9,8,6 (1,2 are the only lower numbers = 20%, the rest are higher = 80)

If the $factor will change, then I want to change the percentage, in that case for example 40% lower numbers, 60% higher numbers.

The idea I have is to put each output in the loop to an array, then check each result and somehow calculate, if there is no 80% of larger numbers, then get random numbers again for those, but this seems to be an overkill.

Is there a simplier solution?

CodePudding user response:

Let's go with the percentages you mention and first generate a random number between 1 and 100. Then the lower number, 1 to 20, have to represent outputs 1 to 5 and the higher numbers, 21 to 100, have to represent output 6 to 10. In PHP that would look like this:

function HighMoreOften()
{
    $percentage = rand(1, 100);
    if ($percentage <= 20) {
        return rand(1, 5);
    } else {
        return rand(6, 10);
    }
}

That should do the trick. You can also convert the percentage you got into the output, this would probably be slightly faster:

function HighMoreOften()
{
    $percentage = rand(1, 100);
    if ($percentage <= 20) {
        return ceil($percentage / 5);
    } else {
        return 6   ceil(($percentage - 20) / 20);
    }
}

but personally I think the first version is easier to understand and change.

CodePudding user response:

To change frequency you gonna need an array of numbers. And a sum to this direction. frequency is the relation of something between an array of things.

$t = 0;

// $factor = 1; // let's say that this means 20%
$factor = 2; // let's say that this means 40%


 if ($factor === 1) { 
     
     for ($i = 1; $i <= 80; $i  ) {
         $t  = rand(1,10);
     }
     for ($i = 1; $i <= 20; $i  ) {
         $t  = rand(6,10);
     }

 } else if ($factor === 2) {
     
     for ($i = 1; $i <= 60; $i  ) {
         $t  = rand(1,10);
     }
     for ($i = 1; $i <= 40; $i  ) {
         $t  = rand(6,10);
     }
     
  
 } else {
     for ($i = 1; $i <= 100; $i  ) {
         $t  = rand(1,10);
     }
 }
 echo round(($t/100), 0);

Something like that! :)

CodePudding user response:

I came with a very simple (maybe creepy) solution, but this works as I wanted:

echo print_r(generate("2"));

function generate($factor) {

$nums=array();

for ($i=0;$i<10;$i  ) {
    
    if ($i<$factor) {$rnd = rand(1,5);}
    else {$rnd = rand(6,10);}

    array_push($nums,$rnd);
   
     } 
     return $nums;
    }

I can also shuffle the final array results, as the lower numbers will be on the beginning always, but in my case it doesn't matter.

  • Related