Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random float number between 0 and 1.0 php [duplicate]

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.

like image 263
Matthew Underwood Avatar asked Jan 04 '13 10:01

Matthew Underwood


People also ask

How do you create a random float between 0 and 1?

Generate a random float number between 0 to 1Use a random. random() function of a random module to generate a random float number uniformly in the semi-open range [0.0, 1.0) . Note: A random() function can only provide float numbers between 0.1. to 1.0.

How can I generate a random number between two numbers in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.

What is Mt_rand function in PHP?

The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.

Does PHP have float?

In PHP, the Float data type is used to set fractional values. A float is a number with a decimal point and can be extended to exponential form. Float is also called a floating-point number. Various ways to represent float values are 3.14, 4.75, 5.88E+20, etc.


1 Answers

mt_rand() / mt_getrandmax(); 

Avoid the rand() function, since it usually depends on the platform's C rand() implementation, generally creating numbers with a very simple pattern. See this comment on php.net

Update: In php 7.1 the rand()has been changed and is now merely an alias of mt_rand(). Therefore it is now ok to use rand(), too.

like image 145
Francois Bourgeois Avatar answered Sep 29 '22 09:09

Francois Bourgeois