Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-26-2007, 02:04 PM
Learning Programmer
 
Join Date: Oct 2006
Posts: 48
Rep Power: 0
Paradine is an unknown quantity at this point
Default Generating Random Numbers with PHP

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

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:

PHP 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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 08-27-2007, 11:57 AM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,306
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default

Where is $total_sum declared?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-27-2007, 01:01 PM
Onur
Guest
 
Posts: n/a
Default

Here:
Quote:
// Return the number
return $total_sum;

}

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-27-2007, 04:09 PM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,306
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
Send a message via AIM to John Send a message via MSN to John
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-27-2007, 07:09 PM
Learning Programmer
 
Join Date: Oct 2006
Posts: 48
Rep Power: 0
Paradine is an unknown quantity at this point
Default

Whoops, hehe, my mistake. I've corrected the code now.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP 4 end of life announcement Jordan News 4 08-30-2007 09:55 AM
Return random numbers without duplicates Paradine PHP Tutorials 0 08-26-2007 02:07 PM
I need to generate a random number in PHP dirkfirst PHP Forum 7 07-05-2006 09:58 PM


All times are GMT -5. The time now is 09:16 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads