Random a number in php from 10 to 100 in php

I would like to write a function which will print a array with a random numbers from 10 to 100 but exclude every number which counts on 5(for example 5,10,15,20 etc.). It should print 50 numbers. I know how to print random numbers from 10 to 100, but how to exclude some of them.

My code here:

function getArray($myLength,$min,$max){
    $arr=array();
    for ($i=0;$i<$myLength;$i++){
        $arr[$i]=mt_rand($min,$max);
   }
    return $arr;
}
function printArray($array){
   foreach($array as $value){
        echo $value, " ";
    }
   echo "<br>";
}
$a=10;
$z=100;
$xz=50;

$myArray=getArray($xz,$a,$z);
printArray($myArray);

Any ideas how to exclude numbers from this array?

Answer

Use the % operator to test if the number is a multiple of 5?

function printArray($array){
   foreach($array as $value){
        if ($value % 5 != 0) {
            echo $value, " ";
        }
    }
   echo "<br>";
}

% returns the remainder when dividing numbers. Multiples of 5 will have a remainder of 0, so this prints all the other numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *