[thelist] srand in php

Andrew Forsberg andrew at thepander.co.nz
Tue Oct 23 16:54:12 CDT 2001


>I'm generating a random number using php - I found the information 
>about how to do it in the php manual and at phpbuilder. The random 
>number is 10 digits long, and is generated like this:
>
>mt_srand((double)microtime()*1000000);
>$key = mt_rand(1000000000,9999999999);
>
>I'm just wondering if anyone can offer any insight on why the random 
>number always begins with 1. The next nine digits appear to be 
>random, but the 40 times or so that I've run this, the first digit 
>has always been 1.

Hi Noah

When I ran your code I got several random numbers whose leftmost 
digit was 2, as well as 1. But, I think the problem is environment 
based. Try running this:

<?php
mt_srand((double)microtime()*1000000);
print (mt_rand(1000000000,9999999999));
print ("<br>");
print mt_getrandmax();
print ("<br>");
print getrandmax();
?>

The maximum random number my environment can generate (with mt_ or 
standard random number generation) is 2147483647. Perhaps yours is 
lower, which would explain why you aren't getting anything with a 
leftmost digit of 2.

I'm not quite sure what to suggest, except perhaps: generate two 5 
digit random numbers, multiply the first 5 digit number by 100,000, 
then add the second 5 digit number to it.

HTH
Andrew
-- 
Andrew Forsberg
---
uberNET - http://uber.net.nz/
the pander - http://thepander.co.nz/




More information about the thelist mailing list