SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random decimal number

    I'm sure I'm being very stupid, however I can't get a random decimal number using the rand() function. I did this somehow in a previous project however I lost my files

    I was trying

    rand(1.1,1.9);

    which doesn't work. How can I do this?

    PHP 4.something

    Thanks

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rand($lower, $upper) / $whatever

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    rand() only accepts ints

  4. #4
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. Just incase anyone is looking for this again I found the following code:

    <?php
    function random_float ($min,$max) {
    return ($min+lcg_value()*(abs($max-$min)));
    }
    ?>

    which works nicely.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •