Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 09-14-2005, 01:36 PM Random numbers
Skilled Talker

Posts: 64
Trades: 0
If I've got this right, the script to generate a random number is:

PHP Code:
<?php
echo rand (0,10)
?>
How do I make this produce a decimal to 2 or 3 decimal places?
Tomos is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-14-2005, 03:15 PM
eburza's Avatar
Super Talker

Posts: 122
Name: Antonio Magdic
Location: Zagreb, Croatia
Trades: 0
try use this function

sprintf();
__________________

Please login or register to view this content. Registration is FREE
eburza is offline
Reply With Quote
View Public Profile Visit eburza's homepage!
 
Old 09-14-2005, 03:20 PM
Skilled Talker

Posts: 64
Trades: 0
How would I use it?

What does it do?
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-14-2005, 11:18 PM
stOx's Avatar
Machine

Latest Blog Post:
Worlds Smallest Car - Peel P50
Posts: 2,111
Name: Matt. (>',')>
Location: London, England.
Trades: 0
to produce a random number with decimal places i would do this.
PHP Code:
<?php

$num 
rand(09);
$dec1 rand(19);
$dec2 rand(19);

echo 
"$num . $dec1 $dec2";
?>
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
stOx is offline
Reply With Quote
View Public Profile Visit stOx's homepage!
 
Old 09-15-2005, 01:29 PM
Skilled Talker

Posts: 64
Trades: 0
Thanks, I hope it works! I'll test it out now
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-16-2005, 09:50 PM
Novice Talker

Posts: 13
Trades: 0
Sprintf would be a waste in this case, and the second example makes three calls to one function. You could use number_format() or any one of those *EDIT* string */EDIT* functions, but *EDIT* they'll just waste processor time */EDIT*.

Option 1. quick and concise
$rand = rand(0,9).'.'.rand(99,999);

Option 2. normally quicker than #1 if your *only* going to echo
echo rand(0,9),'.',rand(99,999);

Option 3. Quicker than any of the above, and simple. I bet you thought basic math would never be useful.
$rand = rand(0,999)/10;

:P

~capiCrimm

Last edited by capiCrimm; 09-16-2005 at 11:15 PM..
capiCrimm is offline
Reply With Quote
View Public Profile
 
Old 09-17-2005, 09:35 AM
Skilled Talker

Posts: 64
Trades: 0
Thanx!
Tomos is offline
Reply With Quote
View Public Profile
 
Old 09-18-2005, 12:30 AM
nick89's Avatar
Super Talker

Posts: 136
Location: Brisbane, Australia
Trades: 0
PHP Code:
$rand mt_rand(1,99) / 100 
nick89 is offline
Reply With Quote
View Public Profile Visit nick89's homepage!
 
Reply     « Reply to Random numbers
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26028 seconds with 12 queries