PHP srand() Function

PHPServer Side ProgrammingProgramming

Definition and Usage

The srand() function is used to seed the random number generaror. Seeding initializes the random number generator. Most random number generators need initial seeding. In PHP, use of srand() function is optional as it is done automatically.

This function doen't have any return value.

Syntax

srand ([ int $seed ] ) : void

Parameters

Sr.NoParameter & Description
1seed
an integer to be used as seed. If not given, a random number is given

Return Values

This function doesn't return any value.

PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

Example

 Live Demo

This example the random number generator is first initialized before employing rand() function−

<?php
   srand(5);
   echo "rand(1,100)=", rand(1,100);
?>

Output

This may produce following result −

rand(1,100)=12

Example

 Live Demo

Following example uses current timestamp to initialize random number generator−

<?php
   srand(time());
   echo "rand()=", rand();
?>

Output

This may produce following result−

rand()=548287992
raja
Published on 30-Jun-2020 10:59:27
Advertisements