tags:

views:

72

answers:

2

I have a script that calls the rand function:

<?php
echo rand(0, 9);
?>

If I run this script multiple times, will I get different numbers out, or will it always be the same? In other words, does PHP seed rand automatically?

A: 

YES! you can get the exact number between 0-9 twice in a row. Whatever you're asking, you'll find it in PHP's rand() manual.

Babiker
+2  A: 

Before version 4.2.0, it will output the same value every time; you're supposed to call srand() to seed it. Starting with 4.2.0, the random number generator is seeded automatically (see the rand() changelog), so you'll get different outputs every time you run the script

Michael Mrozek
+1 for understanding the question :-) You can still use srand manually if you want to seed the RNG with the same number, i.e. for debug purposes, or if you don't trust PHPs builtin seeding algorithm.
Emil Vikström