PHP srand() – Seeding the Random Number Generator

This article explains PHP's srand() function, detailing its purpose of seeding the random number generator, optional seed parameter, lack of return value, and provides a complete example showing how to generate a seed from microtime and produce random numbers.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP srand() – Seeding the Random Number Generator

The srand() function seeds PHP's random number generator with an optional integer seed; if the seed is omitted, a value derived from the current time is used.

Parameters int $seed – optional seed value.

Return value

None (void).

Example

<?php
// seed with microseconds
function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float)$sec + ((float)($usec) * 100000);
}

srand(make_seed());
$randval = rand();
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPrandomseedsrand
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.