Current Location: Home> Latest Articles> How to Generate Random Numbers with Decimals Using PHP's lcg_value() and round() Functions

How to Generate Random Numbers with Decimals Using PHP's lcg_value() and round() Functions

gitbox 2025-06-18

What is the lcg_value() Function?

lcg_value() is a built-in random number generation function in PHP. It returns a floating-point value between 0 and 1. The generated value follows the Linear Congruential Generator (LCG) algorithm, providing good randomness.

For example, the basic code to generate a random number using lcg_value() is as follows:

$random = lcg_value();  
echo $random;  

This code will output a random number between 0 and 1, such as 0.893456 or 0.124567.

How to Generate a Random Number with Decimals?

If you need to generate a random number within a specific range that includes decimals (for example, between 1 and 10), you can use lcg_value() to get a floating-point value and then perform a mathematical operation to scale the value to your desired range. For example:

$min = 1;  
$max = 10;  
$random = $min + (lcg_value() * ($max - $min));  
echo $random;  

This code will generate a random number between 1 and 10, and it will include decimal places.

Using round() to Control Decimal Places

Sometimes, you may not only need a random number with decimals, but also want to control the number of decimal places. To achieve this, you can use PHP's round() function to round the random number. The basic syntax of the round() function is as follows:

round($number, $precision);  
  • $number: The number to be rounded.

  • $precision: The number of decimal places to retain after rounding.

For example, to generate a random number between 1 and 100 with two decimal places, you can write the following:

$min = 1;  
$max = 100;  
$random = $min + (lcg_value() * ($max - $min));  
$random_rounded = round($random, 2);  
echo $random_rounded;  

This code will output a random number between 1 and 100, rounded to two decimal places. For example, it could be 53.12 or 75.56.

Displaying the Random Number on a Web Page

Sometimes, you may want to display the generated random number on a webpage, or pass it to JavaScript. For example, you can pass the generated random number to frontend JavaScript via a URL or store it in the backend. You can implement this as follows:

$min = 1;  
$max = 100;  
$random = $min + (lcg_value() * ($max - $min));  
$random_rounded = round($random, 2);  
<p>// Passing the random number to the frontend via URL<br>
echo "<a href='<a rel="noopener" target="_new" class="" href="https://gitbox.net/random.php?number=$random_rounded&#039;>Click">https://gitbox.net/random.php?number=$random_rounded&#039;>Click</a> to view the generated random number</a>";<br data-is-only-node="">

In this code, we pass the generated random number as a query parameter in the URL to a PHP page. In this example, we replaced the domain with gitbox.net, so you can easily pass the generated random number to the frontend or other pages.

Conclusion

Using lcg_value() to generate random numbers and round() to control the number of decimal places is a simple and effective method, especially when precise control over decimal places is needed. By combining these two functions appropriately, you can generate various random numbers that fit your needs. I hope this article helps you understand how to generate and control random numbers with decimals in PHP. If you have more questions about PHP random number generation, feel free to continue exploring.