PHP Function Reference

PHP lcg_value() Function



The PHP lcg_value() function returns a pseudo random float value between 0.0 and 1.0, inclusive. The function combines two linear congruential generator (lcg) with periods of 231 - 85 and 231 - 249.

Syntax

lcg_value()

Parameters

No parameter is required.

Return Value

Returns pseudo random float value between 0.0 and 1.0, inclusive.

Example:

In the example below, lcg_value() function is used to generate pseudo-random float values.

<?php
//generate 10 pseudo-random float 
//value between 0 and 1
for ($i = 1; $i <= 10; $i++) {
  echo lcg_value()."\n";
}
?>

The output of the above code will be:

0.43624627289115
0.62294218058814
0.2925561890584
0.19630600814257
0.66700470822006
0.75433242654108
0.1120608768236
0.92311637477601
0.99019413299801
0.1975297115566

Example:

Consider one more example where lcg_value() function is used to generate pseudo-random float values in between 5.0 and 10.0.

<?php
//generate 10 pseudo-random float 
//value between 5 and 10
$min = 5;
$max = 10;
for ($i = 1; $i <= 10; $i++) {
  echo ($min + lcg_value()*($max - $min))."\n";
}
?>

The output of the above code will be:

7.8707173015135
9.2671013017022
9.6428466852561
5.302385467896
7.9345581129974
5.5195811951591
5.2732441928208
6.9075435850088
5.7902951218458
8.8287315560067

❮ PHP Math Reference