+ Reply to Thread
Results 1 to 5 of 5

Thread: Generating Random Numbers with PHP

  1. #1
    Learning Programmer Paradine is an unknown quantity at this point
    Join Date
    Oct 2006
    Posts
    48

    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 07:09 PM.

  2. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,883
    Blog Entries
    25
    Where is $total_sum declared?

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

    }

    ?>

  4. #4
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,883
    Blog Entries
    25
    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.

  5. #5
    Learning Programmer Paradine is an unknown quantity at this point
    Join Date
    Oct 2006
    Posts
    48
    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. PHP 4 end of life announcement
    By Jordan in forum News
    Replies: 4
    Last Post: 08-30-2007, 09:55 AM
  2. Return random numbers without duplicates
    By Paradine in forum PHP Tutorials
    Replies: 0
    Last Post: 08-26-2007, 02:07 PM
  3. I need to generate a random number in PHP
    By dirkfirst in forum PHP Forum
    Replies: 7
    Last Post: 07-05-2006, 09:58 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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