Closed Thread
Results 1 to 8 of 8

Thread: I need to generate a random number in PHP

  1. #1
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23

    I need to generate a random number in PHP

    I am making a program and need a random number to select a feature. How do I generate a random number in PHP?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    RobSoftware is offline Programmer
    Join Date
    Nov 2005
    Posts
    143
    Rep Power
    0
    Easy:

    Code:
    <?php
    
    $min = 0;
    $max = 100;
    
    srand((double)microtime()*1000000);  
    echo rand($min,$max); 
    
    ?>
    replace $min and $max with whatever you like.

  4. #3
    RobSoftware is offline Programmer
    Join Date
    Nov 2005
    Posts
    143
    Rep Power
    0
    I forgot to mention, go here for more info:

    http://www.php.net/manual/function.rand.php

  5. #4
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31
    RobSoftware:
    All new versions of php (4+) no longer require srand before rand, it does it automaticly when rand is called.

  6. #5
    NeedHelp Guest
    So the new code would look like:

    Code:
    <?php

    $min 
    0;
    $max 100;

    echo 
    rand($min,$max); 

    ?>

  7. #6
    TkTech's Avatar
    TkTech is offline The Crazy One
    Join Date
    Jun 2006
    Location
    Canada
    Posts
    1,412
    Blog Entries
    1
    Rep Power
    31
    bingo needhelp

  8. #7
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23
    Thank you guys

  9. #8
    Kaabi Guest
    That second code looks much better than the first code. I once wrote a random name generator in C++ (I don't remember how to now, it was when I was trying to learn C++), and it was pretty cool, I only used it once.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Generate an array of 20 random numbers between 1-100...
    By Surfensteve in forum Visual Basic Programming
    Replies: 3
    Last Post: 03-10-2011, 12:37 PM
  2. Using a random function to generate another random funtion
    By Roger in forum General Programming
    Replies: 11
    Last Post: 08-23-2010, 08:07 AM
  3. How do you generate random numbers?
    By leroy in forum Visual Basic Programming
    Replies: 23
    Last Post: 09-04-2009, 09:46 AM
  4. Generate a Random number and have user guess it
    By big-tony in forum C and C++
    Replies: 9
    Last Post: 04-28-2009, 02:15 PM
  5. Generate random points
    By tim838 in forum Java Help
    Replies: 2
    Last Post: 10-16-2006, 04:59 PM

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