lcg_value() is deprecated as of PHP 8.4. For PHP 8.3 and later, use Random\Randomizer::getFloat() when you need a uniformly selected float in a chosen interval.
<?php
$randomizer = new Random\Randomizer();
$value = $randomizer->getFloat(0.0, 1.0);
echo $value;
?>
lcg_value() is retained here for understanding older PHP code. It should not be the starting point for new projects.float lcg_value()
The function returns a pseudo-random float around the interval from 0 to 1. It is not cryptographically secure, and PHP 8.4 deprecates it.
<?php
$value = lcg_value();
echo $value;
?>
Do not scale lcg_value() with multiplication to create integer ranges. Use Random\Randomizer::getInt() or random_int() instead.
<?php
$randomizer = new Random\Randomizer();
$roll = $randomizer->getInt(1, 6);
echo $roll;
?>
On PHP 8.3+, nextFloat() returns a float from the right-open interval [0.0, 1.0), which is convenient for probability checks.
<?php
$randomizer = new Random\Randomizer();
$eventOccurs = $randomizer->nextFloat() < 0.3;
echo $eventOccurs ? 'Event occurs' : 'Event does not occur';
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.