Welcome to WebmasterWorld Guest from 54.196.116.152

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Something puzzling me in PHP

rand() function

     
7:54 pm on Mar 26, 2001 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 6, 2000
posts:904
votes: 0


A while ago I pinched a snippet of PHP from somewhere to use in a session id generator. It all worked fine, but as I was only just starting out in PHP I didn't investigate too deeply how it worked. This construct is now puzzling me:

$foo=rand()%(strlen($pool));

The effect is to choose a random number between zero and the character length of string $pool - yet I can't find any documentation on this form of rand() anywhere... the manual for PHP4 specifies rand(min_value, max_value) for this kind of thing.

Does anyone know if this is a correct construct or a hangover from an earlier version of PHP?

2:11 am on Mar 28, 2001 (gmt 0)

Full Member

joined:Jan 24, 2001
posts:230
votes: 0


It's not a hangover. If no seed value is provided, the rand() function is automatically seeded with a value of 1.
10:23 am on Mar 29, 2001 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 6, 2000
posts:904
votes: 0


Thanks Froggyman. It's the % sign that puzzles me - I take it this is just a different method of specifying the maximum value?