rand()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

rand()

Post by cbrian »

Code: Select all

<?php
$min = 0.090000 * $lrand;
$max = 0.100000 * $lrand;
$gain = rand($min, $max);
?>
$lrand is stated above in the code, it is not the problem.

Most of the time this equals 0, but when i raise $lrand it goes to 1 sometimes. How can I used rand() to use decimals and not round to nearest whole number?
Last edited by cbrian on Thu Mar 10, 2005 6:44 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

int rand ( [int min, int max] )
notice that it says int, as in integer.

Scale the values past the decimal level you wish to rand against.. alternately.. you can use the rand as a scalar value. such as

Code: Select all

$scalar = rand(0,10000) / 10000;
$min = 0.9;
$max = 0.1;
$level = ($max - $min) * $scalar + $min;
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post by cbrian »

Okay, thanks.
Post Reply