lcg_value(): Combined linear congruential generator

Generate a pseudo-random float with lcg_value()

lcg_value() returns a pseudo-random floating-point number greater than 0 and less than 1. It is not appropriate when unpredictability or security is required.

$value = lcg_value();
echo $value; // value between 0 and 1

Start with this current pattern. The examples below are retained for additional variations, output and reference.

echo lcg_value();
Out put is ( may change ) , refresh this page and check.
<?Php 
echo lcg_value(); ?>
Syntax
flot lcg_value ( void)
The output is pseudo random number in the range of (0, 1).

Example 1: Generating a Range of Random Values Using lcg_value()

<?php
$min = 10;
$max = 100;

// Generate a random number between 10 and 100
$randomValue = $min + (lcg_value() * ($max - $min));

echo "Random value between 10 and 100: " . $randomValue;
?>

Example 2: Using lcg_value() for Weighted Randomness

<?php
$weight = 0.8; // Adjust weight to favor higher values

// Generate a weighted random value
$weightedRandom = lcg_value() * $weight;
echo "Weighted Random Value: " . $weightedRandom;
?>

Example 3: Simulating Dice Rolls with lcg_value()

<?php
function rollDice() {
    return 1 + floor(lcg_value() * 6);
}

echo "You rolled a: " . rollDice();
?>

Example 4: Randomizing Color Brightness with lcg_value()

<?php
function randomColor() {
    $brightness = 100 + (lcg_value() * 155); // Adjusts brightness between 100 and 255
    return "rgb($brightness, $brightness, $brightness)";
}

echo "Random color: " . randomColor();
?>

Example 5: Generating a Random Probability for Events

<?php
function randomEvent($probability) {
    return lcg_value() < $probability ? "Event occurs" : "Event does not occur";
}

echo randomEvent(0.3); // 30% chance of event occurring
?>

Math Functions base_convert(): Convert number From any base to other
decbin() Binary equivalent of decimal number


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