+ Reply to Thread
Results 1 to 5 of 5

Thread: Generating Random Numbers with PHP

  1. #1
    Paradine is offline Learning Programmer
    Join Date
    Oct 2006
    Posts
    48
    Rep Power
    0

    Generating Random Numbers with PHP

    Below is a simple function to generate random numbers with PHP:

    Code:
    <?php
    ##########################################
    # Input: Minimum number, 
    #    Maximum number
    ##################################
    function randomNumber($min$max)
    {
        
    // setup our seed
        
    srand((float) microtime() * 10000000);
        
        
    // Generate the number
        
    $random rand($min,$max);
        
        
    // Return the number
        
    return $random;

    }

    ?>
    Usage:

    Code:
    $random_number randomNumber(0,50); 
    Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
    Last edited by Paradine; 08-27-2007 at 05:09 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    Where is $total_sum declared?

  4. #3
    Onur Guest
    Here:
    // Return the number
    return $total_sum;

    }

    ?>

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    Quote Originally Posted by Onur View Post
    Here:
    I understand that it is returned in that line, but it is never set to a value, so unless I am missing something, it will always return null.

  6. #5
    Paradine is offline Learning Programmer
    Join Date
    Oct 2006
    Posts
    48
    Rep Power
    0
    Whoops, hehe, my mistake. I've corrected the code now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. NASM Generating random numbers
    By untitled_1 in forum Assembly
    Replies: 2
    Last Post: 10-30-2010, 10:24 AM
  2. C# Tutorial: Generating Random Numbers.
    By CommittedC0der in forum CSharp Tutorials
    Replies: 21
    Last Post: 07-25-2010, 10:21 AM
  3. Getting Random numbers in VB.NET
    By Vswe in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 11-13-2009, 08:15 AM
  4. Generating numbers
    By addicteddrug in forum C and C++
    Replies: 6
    Last Post: 12-07-2008, 01:49 AM
  5. Generating random background...
    By bentoh in forum General Programming
    Replies: 5
    Last Post: 01-24-2008, 05:47 AM

Tags for this Thread

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