PDA

View Full Version : Does rand() work on floats?


srule_
10-24-2007, 10:12 PM
Hey, i need to generate a random number between 70% and 100%

will rand(.7, 1) work?

GJay
10-24-2007, 10:36 PM
well, in the time it's taken you to wait you could have tested it to see, or had a look at what the manual (http://php.net/rand) has to say:


Description
int rand ( [int $min, int $max] )

If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).

(emphasis mine)

srule_
10-24-2007, 10:42 PM
Ya i read the manual,

but .7 is between 0 and rand_max, so i was wondering if this would work, or if .7 would take the value of 0.

but i guess the reference of integer answers my questions...

any functions like rand that take floats?

CFMaBiSmAd
10-24-2007, 10:52 PM
A little elementary school math will solve your problem (tested) -
echo rand(70,100)/100;

marek_mar
10-24-2007, 11:16 PM
mt_rand() is preferred.