mt_srand

(PHP 3 >= 3.0.6, PHP 4, PHP 5)

mt_srand -- 播下一个更好的随机数发生器种子

说明

void mt_srand ( int seed )

seed 来给随机数发生器播种。从 PHP 4.2.0 版开始,seed 参数变为可选项,当该项为空时,会被设为随时数。

例子 1. mt_srand() 范例

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

注: 自 PHP 4.2.0 起,不再需要用 srand()mt_srand() 函数给随机数发生器播种,现已自动完成。

参见 mt_rand()mt_getrandmax()srand()


add a note add a note User Contributed Notes
oxai NOSPAM post dot htnet dot hr
10-Sep-2006 02:14
try this instead(!):

<?php

// randomizes MT's seed once per process.

function randomizeProcessSeed()
{
   static
$thisProcessHasBeenInitialized;

   if(
$thisProcessHasBeenInitialized ) return;

   list(
$usec, $sec) = explode(' ', microtime());
  
mt_srand( (10000000000 * (float)$usec) ^ (float)$sec );
  
  
$thisProcessHasBeenInitialized = true;
}
randomizeProcessSeed();
?>
gigs
13-Jan-2006 03:13
used the little script from mrcheezy at hotmail dot com and got much better results using

mt_srand(crc32(microtime()));
vbassassin at coderheaven dot com
01-Dec-2005 12:57
"Better still: Use a 31-bit hash of microtime() as the seed. "

Correct me if i am wrong, but woudlnt using microtime() still limit the total seeds to 1,000,000 again? Since the 31-bit hash will always give the same hash for the same number, and in the microtime() function you could have 1,000,000 or less numbers. So in effect your still no better off at all :-p

Best regards,
scott

PS: I actually agree that PHP has pretty much resolved the issue and got as close as anyones going to get to solving the seeding issue by introducing the "Mersenne Twister" algorithm which creates a much larger pool than 1,000,000 numbers. Just because the mt_srand() function exists doesnt mean you HAVE to use it ;-) use it if you NEED a specific list of the same numbers (comes in handy for encryptions with passwords ;-)
12-Sep-2005 09:03
It's better to use the following method instead of the one in the documentation metioned:

<?php
mt_srand
((double)(microtime() ^ posix_getpid()));
?>

Otherwise people requesting the script at the same time could get the same generated number.
Schrecktech
21-Jan-2005 10:16
::: My seeding report :::

For FreeBSD 4.8-STABLE PHP 4.3.10 it was good enough and faster to not seed at all...although I did not test the random pseudo terminal. 

In detail only seeding once by a previous seed using the hex version (case 3 below) for larger numbers and longer run times faired the best.  For small numbers it was better to not seed at all (let alone faster).  Play with the $loop and $max numbers to see...

<?
function get_a_good_seed
(
  
$choice = 3
)
{
   static
$seed;
  
$seed = 0;
   switch(
$choice)
   {
       case
1:
          
// from php.net documentation.
          
list($usec, $sec) = explode(' ', microtime());
          
$seed = (float) $sec + ((float) $usec * 1000000);
           break;
       case
2:
          
// from high primary number.
          
$seed = (double)microtime()*1000003;
           break;
       case
3:
          
// from a good random source.
          
$seed = hexdec(substr(md5(microtime()), -8)) & 0x7fffffff;
           break;
       case
4:
          
// from random terminal
           // NOT TESTED
          
break;
       default:
           break;
   }
   return(
$seed);
}
function
load_last_random
(
)
{
   static
$last;
   static
$handle;
  
// NOTE: create this file with a random integer...
  
$handle = fopen("random.txt", "r");
   while (!
feof($handle)) {
      
$last = fgets($handle, 4096);
   }
  
fclose($handle);
   return(
$last);
}

function
save_last_random
(
  
$save = 0
)
{
  
$handle = fopen("random.txt", "w");
  
fwrite($handle, $save);
  
fclose($handle);
}

function
seed_random_number_generator
(
  
$type = 3,
  
$ignore_generated_already = FALSE
)
{
   static
$generator_seeded = FALSE;
   if(
$ignore_generated_already || !$generator_seeded)
   {
      
// 1) load last with time...force integer.
       // 2) generate a new random seed and save for next time...
      
mt_srand(( load_last_random()+get_a_good_seed($type) ) & 0x7fffffff);
      
save_last_random(mt_rand());
      
$generator_seeded= TRUE;
   }
}
?>
fasaxc at yahoo dot co dot uk
25-Jul-2003 11:01
In fact, here's an even better function than the one below assuming your install provides a random entropy daemon and you're running *nix (to check for the former type "head -c 6 /dev/urandom" on the command line if available - if you get 6 random characters you're set). N.B. php must be able to find the head program so it must be in your path and allowed if you're running safe mode.

The functions db_set_global() and db_get_global() I use to set/get a variable from a central database but you could save/restore the variable from a file instead or just use the function get_random_word().

<?

####################################
## returns a random 32bit integer.
## Passing a parameter of True gives a better random
## number but relies on the /dev/random device
## which can block for a long time while it gathers
## enough random data ie. DONT USE IT unless
##  a) You have an entropy generator attatched to
## your computer set to /dev/random -OR-
##  b) Your script is running locally and generating
## a good random number is very important
####################################
function get_random_word($force_random=False) {
   if (
$force_random) {
      
$u='';
   } else {
      
$u='u';
   }
  
$ran_string=shell_exec("head -c 4 /dev/{$u}random");
  
$random=ord(substr($ran_string,0,1))<<24 |
          
ord(substr($ran_string,1,1))<<16 |
          
ord(substr($ran_string,2,1))<<8 |
          
ord(substr($ran_string,3,1));
   return
$random;
}

--
EITHER - IF YOU'VE SET UP A DATABASE OF GLOBAL VARIABLES--

## If the seed is found in the database
if ($seed=db_get_global('
seed')) {
   # use mt_rand() to get the next seed
   mt_srand($seed);
   # then XOR that with a random word
   $seed=(mt_rand() ^ get_random_word());
} else {
## Make a completely new seed (First Run)
   # Generate the seed as a proper random no using /dev/random
   $seed=get_random_word(True);
   mt_srand($seed);
}

db_set_global('
seed',$seed);

--OR JUST--
mt_srand(get_random_word());

?>
fasaxc at yahoo dot com
18-Jul-2003 07:14
The best way to ensure a random seed is to do the following:
To start:
   1) get your initial seed with mt_srand(microtime() * 1000000)
   2) generate a random no. $random=mt_rand()
   3) save this number in a file (or database or whatever so that it is available next time the page is loaded)

 Now, for each time your script is loaded :
   1) load the value you saved above and do $new_seed=($random+(microtime() * 1000000))%pow(2,32)
   2) mt_srand($new_seed);
   3) generate a new random no. $random=mt_rand()
   4) save that number back in the file/database

This procedure takes advantage not only of the randomness of microtime() but of all the previous calls to microtime() so your seed becomes better and better with time. It also generates good seeds even on platforms where microtime() doesn't take all the values it can.

Just using microtime() * 1000000 only results in 1000000 possible seeds (and less on some platforms as noted) - the function above gives 2^32 seeds with an avelanche effect accross multiple executions.
mrcheezy at hotmail dot com
18-Feb-2003 06:14
Very good points above on seeds, thank you. If you would like to test a seed try using the code below. It will take between 5 and 20 seconds depending on your system and then will spit out the number of reused keys out of 100,000 attempts.

;  for ($i=0; $i<100000; $i++) {
;    mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
;    $rand = mt_rand();
;
;    ($arr[$rand] == '1') ? $k++ : $arr[$rand] = '1';
;  }
mattb at columbia dot edu
12-Dec-2002 04:54
This is effectively the same thing, but it uses a more efficient bit mask instead of a substring. Here's the verbose version:

   function mkseed()
   {
       $hash = md5(microtime());
       $loWord = substr($hash, -8);
       $seed = hexdec($loWord);
       $seed &= 0x7fffffff;

       return $seed;
   }

Or, if you're a real hacker, this is a more concise version:

   function mkseed()
   {
       return hexdec(substr(md5(microtime()), -8)) & 0x7fffffff;
   }

Note, the range on the md5 sum is really arbitrary. You could use whatever portion you like, just so long as it's 32 bits.

Now just call mt_srand() (once) as follows:

   mt_srand(mkseed());

Of course, unless you wanted the mkseed() function for something else, you could skip all that jive and just do this somewhere in a global include file:

   <?php

  
/*
       Global include file. Make sure this gets included before using
       mt_rand().
   */

  
if (!isset($_MyApp_MtSrand)
       || !
$_MyApp_MtSrand)
   {
      
$_MyApp_MtSrand = true;
      
mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
   }

  
?>
ghaecker_no_spam at idworld dot net
21-Sep-2002 04:20
The range of unique seeds using this method is a bit over 2 billion.  This approach also prevents re-seeding.

function seed_mt_rand() {
static $done;
if (!$done) {
$hash = md5(microtime());
$length = ((substr($hash,0,1) < '8') ? 8 : 7 );
mt_srand((int)base_convert(substr($hash,0,$length),16,10));
$done = TRUE;
}
}
maxim at php dot net
14-Jun-2002 05:57
to : l_rossato@libero.it

doing ...

list($usec,$sec)=explode(" ",microtime());
$unique = mt_srand($sec * $usec);

theoretiaclly, makes just as much sense as

list($usec,$sec)=explode(" ",microtime());
$unique = $usec + 0;

Once every while, depending on the microsecond resolution of your computer, the millisecond value will be a zero (0), and as I hope you know, in mathematics, any number multiplied by a zero becomes a zero itself.

(x * 0 = 0)

In real life, on a good machine, with a resolution to 1 million miliseconds per each second (i.e: Win2k server), you will be reduplicating your unique ID each million's ID issued. This means if you use it as your cookie encryption algorithm or a visitor ID, you will not exceed some million instances.

Futhermore, if that would be for a software development that you re-distribuite, installed on some weird old PC, where resolution can be as small as 100 milliseconds per second - a code with this uniqueness algorithm just wouldn't last any long.

Good Luck,

Maxim Maletsky
maxim@php.net

PHPBeginner.com
changminyang at hananet dot net
21-Feb-2002 06:00
list($usec,$sec) = explode(" ",microtime());

/* Test: Each get rand sequence are 10time. */
/* ex) 5.3point meaning 5point integer + 3point decimal */

// case A:
// 5.0point - 1time
// 6.0point - 9time
$rand = (double)microtime()*1000000;

// case B:
// 8.6point - 1time
// 9.4point - 1time
// 9.5point - 7time
// 10.3point - 1time
$rand = (double)$sec * $usec;

// My case A:
// 8.0point - 10time
$rand = explode(".",$usec * $sec);
$rand = (double)substr($rand[0]*$rand[1],0,8);

// My case B:
// 9.0point - 9time
// 10.0point - 1time
$rand = explode(".",$usec * $sec);
$rand = $rand[0] + $rand[1];

mt_srand($rand);
srand($rand);

// P.S> My previous note is has wrong lines, sorry about it.  This is right.
l_rossato at libero dot it
13-Nov-2001 12:47
Try this much simpler approach:
list($usec,$sec)=explode(" ",microtime());
mt_srand($sec * $usec);

This won't cause an overflow during the float-to-integer conversion.
Of course, if you only need ONE random number just take $sec * $usec! Otherwise, call mt_srand just once at the start of your program.
roundeye_no_spam at roundeye dot net
04-Nov-2001 09:23
Since I've seen this problem in so many other manual comments I'm writing a note here.  Basically, calling mt_srand() every time you call mt_rand() defeats the whole purpose of a generator like the Mersenne Twister, and will greatly reduce the quality of the number stream you produce.  For cryptographic or security purposes this is A Bad Thing.
<br>
DON'T do this:
<br>
function bad_pick_random_number($min, $max) {
  mt_srand();
  return mt_rand($min, $max);
}
<br>
Instead, call mt_srand() *once* early in the execution of your program and pick your numbers separately after that:
<br>
function good_pick_random_number($min, $max) {
   return mt_rand($min, $max);
}
<br>
Of course, if you're doing that sort of thing you might as well just call mt_rand() directly.
trobinson-a-t-gksystems-d-o-t-com
02-Nov-2001 06:15
I see a problem with seeding with:
mt_srand((double)microtime()*1000000);
Too few seeds.
This chooses the seed from a space of only one million seeds. This is a shame, because the Mersenne Twister algorithm has a period of 2**19937-1.
So of all the possible starting points in the MT sequence, the above seed only chooses at random from among 1000000 starting points, or about 0.000...0001% of the possible starting points, where "..." is a string of about 5986 more zeros!
In real life, this means that if you note the first few random numbers, when you see the same first few numbers again, the chances are very good that you have the same seed and the rest of the sequence will be the same too.

If you generate a random "unique" 100-character key for say cookie use, seeding as above, you will likely get a duplicate key after only 1000 keys or so.
Better: To supplement the results from mt_rand, also hash in the entire contents of the microtime() string.
Better still: Use a 31-bit hash of microtime() as the seed.

Problem 2. Way too few seeds on some hardware. It is bad to rely on a clock ticking every microsecond. I have used computers where 100ms was the finest-resolution clock available. That would be just 10 different seeds, as the results from (double)microtime() would look like:
0.10000000
0.20000000
etc.
php_public at macfreek dot nl
07-Oct-2001 11:57
The seed is not good. The make_seed() function returns a big number (around 1.0E14), this number is bigger then 2147483647, the max. size of an integer. If the result of make_seed is first converted to an integer, which -on my setup- always becomes -2147483648. So, the above code always returns the same seed and I always end up with the same random numbers.

Note: This might be related to a bug noted on http://www.php.net/manual/en/language.types.integer.php. But I couldn't confirm this, since it will be fixed in PHP 4.0.7, but that isn't released yet at the time of writing)

I was also not able to fix it by return the seed modulo 2147483647.
So for now, I just suggest change the seed function as:

function make_seed() {
   return (double)microtime()*1000000;
}

See also: uniqid()