lcg_value(): Combined linear congruential generator

Use Random\Randomizer for new float generation

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;
?>
Legacy function: lcg_value() is retained here for understanding older PHP code. It should not be the starting point for new projects.

Legacy lcg_value() syntax

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.

Legacy example

<?php
$value = lcg_value();
echo $value;
?>

Generate an integer in a range

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;
?>

Probability check with nextFloat()

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';
?>

Math Functions random_int(): secure random integers mt_rand(): legacy/global Mersenne Twister API


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer