W3docs

lcg_value()

Today, we will discuss the lcg_value() function in PHP. This function is used to generate a pseudo-random number using the linear congruential generator (LCG)

The lcg_value() function in PHP is a built-in function that generates a pseudo-random float using the linear congruential generator (LCG) algorithm. It takes no parameters and relies on a fixed internal seed to produce its sequence. This page explains what the function returns, how to use it, how to scale its output to a range, and when to reach for a more secure alternative instead.

What Is the lcg_value() Function?

A linear congruential generator is a classic, lightweight algorithm for producing a sequence of numbers that look random but are fully determined by a starting seed. PHP wraps this in lcg_value(), which returns a float in the range (0, 1) — that is, greater than 0 and less than 1.

Two characteristics matter in practice:

  • No arguments. You call it as lcg_value() with empty parentheses; passing arguments has no effect.
  • Float result. It always returns a float, never an integer. To get an integer, you scale the result yourself (shown below).

How to Use the lcg_value() Function

Using the lcg_value() function in PHP is straightforward. Here is a basic example:

php— editable, runs on the server

In this example, we call the lcg_value() function to generate a pseudo-random number. We then store the result in a variable and output it to the screen.

Generating Numbers in a Specific Range

Since lcg_value() returns a float between 0 and 1, you can scale it to fit a desired range or generate integers:

<?php
// Generate a random integer between 1 and 100
$min = 1;
$max = 100;
$random_int = (int)($min + lcg_value() * ($max - $min + 1));

echo $random_int;
?>

Important Notes

  • Not Cryptographically Secure: The LCG algorithm is predictable and should not be used for security-sensitive tasks like generating tokens, passwords, or session IDs. For cryptographic purposes, use random_int() or random_bytes().
  • Fixed Seed: The function uses a fixed internal seed, meaning the sequence of numbers will be identical across different script executions unless the PHP process is restarted.
  • Range is exclusive: The returned value never equals exactly 0 or 1, so the scaled-integer formula above can safely include both endpoints of your $min$max range.
  • Deprecated in PHP 8.4: As of PHP 8.4, lcg_value() is deprecated in favor of \Random\Randomizer::getFloat(). It still works but emits a deprecation notice, so prefer the newer API in fresh code.

lcg_value() vs. Other Random Functions

lcg_value() is one of several randomness helpers in PHP. Choose based on what you need:

  • For random integers in a range, rand() and mt_rand() are usually more convenient than scaling lcg_value() by hand.
  • To learn the upper bound those generators can return, see getrandmax() and mt_getrandmax().
  • For reproducible sequences during testing, seed the Mersenne Twister generator with mt_srand().
  • For security-sensitive randomness, prefer random_int() over all of the above.

Conclusion

The lcg_value() function provides a simple way to generate pseudo-random floats in PHP. While it is useful for basic simulations or non-security applications, modern PHP development typically favors random_int() for better randomness and security. We hope this guide helps you understand how to use lcg_value() effectively in your projects.

Practice

Practice
What does the lcg_value() function in PHP do?
What does the lcg_value() function in PHP do?
Was this page helpful?