Forum Moderators: coopster

Message Too Old, No Replies

Random Number in PHP

Why does this code not work?

         

phidentity

7:15 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



<?php

srand( microtime() * 1000000);
$num = rand(1,100);
echo("Random Number:".$num."<br/>");

?>

Every time, it generates number 1... Why?

Jon

KMxRetro

7:33 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



I'm not sure about why it's generating the number 1, but I've started using mt_rand of late. Much quicker apparently, when generating lots of numbers...and you don't have to seed it.

<?
$num = mt_rand(1, 100);
echo("Random Number:".$num."<br/>");
?>

That should do the job...

Romeo

7:42 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



Jon,

hmm ... I just took your code and let it run ... works fine here (Linux 2.4.19, apache 1.3.26, PHP 4.2.2):
19 - 37 - 46 - 42 - 51 - 96 ... its OK for me.

Regards,
R.

phidentity

8:32 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



Hmm.

On mine it just does 1,1,1,1.

I have phpTriad, with an slightly older apache/php possibly.

Could this be it?

Jon

phidentity

8:36 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



KMxRetro, tried it, didn't work :(

I think it must be to do with my PHP/apache.

Jon

phidentity

8:43 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



PHP Version 4.0.5
Apache 1.3.14

WindowsXP

Jon

coopster

8:51 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think you still need to seed the better random number generator at that level. Since PHP 4.2.0, the seed becomes optional and defaults to a random value if omitted. Have you tried the example from the manual [php.net]?

<?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();
?>

KMxRetro

8:53 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



I wonder if something is set on the server that rounds numbers up or something.

Try this...

<?php

srand( microtime() * 1000000);
$num = rand(1,100);
$num = $num*100;
echo("Random Number:".$num."<br/>");

?>

phidentity

9:04 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



KMxRetro, it always says '100' (and did on the last one, I copied the code for my needs (1,4) and it always displayed (4).

and Coopster, I echoed $randval, and nothing displayed.

Trust me to have a problem nobody else has!

Thanks for all your effort guys :)

Jon

coopster

9:11 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Thinking more about your issue, I read this on the mt_rand() [php.net] page:

Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the rand() function. The mt_rand() function is a drop-in replacement for this. It uses a random number generator with known characteristics using the Mersenne Twister, which will produce random numbers that should be suitable for seeding some kinds of cryptography (see the home page for details) and is four times faster than what the average libc provides.

So, first off, you are probably better off using mt_rand(). You may also want to echo the mt_getrandmax [php.net] to return the maximum value that can be returned by a call to mt_rand() on your system just to make sure it isn't 1 ;)

I wonder if the 1 being returned every time indicates a numeric value or boolean

true
...?

phidentity

9:13 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



Ah I see.

I'm sort of new to PHP (I did it last year, and re-learning it now).

Would it be possible for you to do some sample code for the above explanation. (thanks!).

I am really happy with this board, everyone is so polite and helpful.

Jon

coopster

9:54 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You mean display the value of mt_getrandmax? Sure,

<?php
print 'mt_getrandmax is: ' . mt_getrandmax();
?>

There are a few quirks and bugs reported at that level of PHP (4.0.5):
Problem in rand() on win2000 (or all win32?) [bugs.php.net]
rand() without srand() doesn't work with certain php.ini [bugs.php.net]

I would recommend updating your PHP for one thing, not just for this rand issue, but there are many other reasons for keeping current with your PHP version levels, security being primary. I realize this may not solve your problem, but it's worth a shot instead of battling a possible bug.

phidentity

9:56 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



Cheers, I will do this now :)

Thanks!

Jon

coopster

10:11 pm on Jan 15, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Great, please come back and let us know if it resolved your issue!

phidentity

10:13 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



It did :) Upgraded my PHP from php.net, and now it works fine.

Wohooo :)

Thank you all!

Jon