Possible Duplicate:
Random Float in php
Is it possible to create a random float number between 0 and 1.0 e.g 0.4, 0.8 etc. I used rand but it only accepts integers.
Possible Duplicate:
Random Float in php
Is it possible to create a random float number between 0 and 1.0 e.g 0.4, 0.8 etc. I used rand but it only accepts integers.
will do that.
In case you want only one decimal place (as in the examples from the question) just
round()
the value you get......or calculate a number between 0 and 100 and divide by 10:
Try this
What about simply dividing by 10?
how about this simple solution:
or
According to the PHP documentation, if you're using PHP 7 you can generate a cryptographically secure pseudorandom integer using random_int
With that said, here's a function that utilizes this to generate a random float between two numbers:
Although random_int() is more secure than mt_rand(), keep in mind that it's also slower.
Cast to float* and divide by
getrandmax()
.* It seems that the cast is unnecessary in PHP's arbitrary type-juggling rules. It would be in other languages, though.