Generate Random Floating-Point Number in Range using PHP

mt_rand & mt_getrandmax functions

<?php

function generateRandomFloat(float $minValue, float $maxValue): float
{
    return $minValue + mt_rand() / mt_getrandmax() * ($maxValue - $minValue);
}

$minValue = 10.5;
$maxValue = 20.5;
$randValue = generateRandomFloat($minValue, $maxValue);

echo $randValue;

Leave a Comment

Cancel reply

Your email address will not be published.