Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Web Development Forum > PHP Forum

PHP Forum Use this forum to discuss all aspects of PHP Development. PHP is a server-side, cross-platform, HTML embedded scripting language that lets you create dynamic web pages.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-19-2006, 05:01 PM
Programming Professional
 
Join Date: May 2006
Posts: 336
Rep Power: 0
dirkfirst is an unknown quantity at this point
Default 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?
__________________
DirkFirst
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-19-2006, 06:42 PM
Programmer
 
Join Date: Nov 2005
Posts: 143
Rep Power: 0
RobSoftware is an unknown quantity at this point
Default

Easy:

Code:
<?php

$min = 0;
$max = 100;

srand((double)microtime()*1000000);  
echo rand($min,$max); 

?>
replace $min and $max with whatever you like.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-19-2006, 06:49 PM
Programmer
 
Join Date: Nov 2005
Posts: 143
Rep Power: 0
RobSoftware is an unknown quantity at this point
Default

I forgot to mention, go here for more info:

http://www.php.net/manual/function.rand.php
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-20-2006, 08:01 AM
TkTech's Avatar   
Speaks fluent binary
 
Join Date: Jun 2006
Posts: 1,163
Blog Entries: 2
Rep Power: 19
TkTech is on a distinguished road
Send a message via MSN to TkTech
Default

RobSoftware:
All new versions of php (4+) no longer require srand before rand, it does it automaticly when rand is called.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
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 06-22-2006, 02:34 PM
Programming God
 
Join Date: May 2006
Posts: 526
Rep Power: 15
NeedHelp is on a distinguished road
Default

So the new code would look like:

PHP Code:
<?php

$min 
0;
$max 100;

echo 
rand($min,$max); 

?>
__________________
I Need Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 06-22-2006, 02:54 PM
TkTech's Avatar   
Speaks fluent binary
 
Join Date: Jun 2006
Posts: 1,163
Blog Entries: 2
Rep Power: 19
TkTech is on a distinguished road
Send a message via MSN to TkTech
Default

bingo needhelp
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
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
  #7 (permalink)  
Old 06-29-2006, 11:59 AM
Programming Professional
 
Join Date: May 2006
Posts: 336
Rep Power: 0
dirkfirst is an unknown quantity at this point
Default

Thank you guys
__________________
DirkFirst
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-05-2006, 09:58 PM
Kaabi
Guest
 
Posts: n/a
Default

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.
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
Generating Random Numbers with PHP Paradine PHP Tutorials 4 08-27-2007 07:09 PM
Floating Point random number generator Paradine PHP Tutorials 0 08-26-2007 02:09 PM


All times are GMT -5. The time now is 09:24 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