Lost Password?

  #1 (permalink)  
Old 08-26-2007, 02:04 PM
Paradine Paradine is offline
Learning Programmer
 
Join Date: Oct 2006
Posts: 48
Rep Power: 7
Paradine is on a distinguished road
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

Sponsored Links
  #2 (permalink)  
Old 08-27-2007, 11:57 AM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,731
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

Where is $total_sum declared?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
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   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,731
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM 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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
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
Paradine Paradine is offline
Learning Programmer
 
Join Date: Oct 2006
Posts: 48
Rep Power: 7
Paradine is on a distinguished road
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

Sponsored Links
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 Programming 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 11:38 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads