#1
  1. No Profile Picture
    timboy
    Guest
    Devshed Newbie (0 - 499 posts)
    I am using the rand() function in php3. Without arguments it seems to behave as advertised, returning an integer between 0 and getrandmax(). It seems to behave the same way with arguments, though, e.g. rand(1,15) -> 1774965089, rather than a number between 1 and 15.
    This is not because I haven't seeded the generator --- I use srand as in the manual before I start making calls to rand.
    Also, this would OK if there were a modulus function (e.g. rand() mod 15), but I don't see one in the math section of the manual.
    Any help appreciated.

    Timboy
  2. #2
  3. No Profile Picture
    timboy
    Guest
    Devshed Newbie (0 - 499 posts)
    Ooops, modulus is an operator ('%').
    rand() still seems to ignore its args, but if anyone else has the same problem, a workaround is something like

    function rand_with_args($min, $max)
    {
    return ($min + rand() % (1 + $max - $min));
    }

    Timboy

  4. #3
  5. No Profile Picture
    timboy
    Guest
    Devshed Newbie (0 - 499 posts)
    Replying to myself once again --- the rand() bug was apparently fixed as of PHP version 3.0.7.

    Timboy

Similar Threads

  1. Parseing shell script arguments
    By karlovac in forum Linux Help
    Replies: 8
    Last Post: February 4th, 2004, 02:10 PM
  2. Replies: 2
    Last Post: February 3rd, 2004, 07:46 AM
  3. Replies: 11
    Last Post: January 14th, 2004, 01:57 AM
  4. Best way to handle command line arguments
    By defjamninja in forum C Programming
    Replies: 5
    Last Post: January 4th, 2004, 08:22 PM
  5. How do you pass arguments to an external javascript function?
    By WCastroL in forum JavaScript Development
    Replies: 5
    Last Post: July 2nd, 2003, 04:48 PM

IMN logo majestic logo threadwatch logo seochat tools logo