Be a loyal visitor by joining our Telegram channel! Join Now

How to Get Random Float in PHP – Best Guide

- Advertisement -

If you are a PHP developer you will realize that the rand() function only accepts integers (whole numbers), this becomes a problem if you want to generate random floats in PHP.

We may not know the use case of your code, but we will cover everything that will enable you to generate random float in PHP, either between 0 and 1 or more.

There are several functions in PHP that allows one to get a random number but almost all of them are in the data types integers and byte. Some of these functions include; rand(), mt_rand(), random_bytes() and random_int(). With these mentioned functions the ones that are mostly used to generate random numbers are the rand() and the mt_rand().

Now let us look at the rand() and the mt_rand() functions before moving on but you can skip the next topic to what you are looking for 😊.

The rand() and mt_rand() function

But these functions are almost the same and can be used interchangeably except that one is obsolete and is not recommended to use in a PHP version higher than 7.1.

The rand() function is advised to be obsolete after the PHP 7.1 update and served as an alias of mt_rand(). However, if you are using PHP versions between 4 and 7.1 you can use but not recommended.

The Mersenne Twister random number generator which is the mt_rand() is what is recommended after the release of PHP 7.1 and supports version 4 and above. So it makes it a better option when you want to generate random integers in PHP.

- Advertisement -

In this guide, we will use the mt_rand() function.

How to Get random float in PHP

The simplest way to generate random floats in PHP is to use the mt_rand() function and divide it by 10 or any other powers of ten.

Now let us see this code that will generate a random float from 0 to 1

<?php

$randomFloat  = mt_rand(0, 10) / 10;
echo $randomFloat;

?>

//This is displayed: 0.6

The above code displayed 0.6 when we ran it. This means it will pick any random decimal from 0 to 1. And take note that the code above can only give you one decimal place float. So the outcomes could be; 0, 0.1, 0.2, 0.3, 0.4, 0.5,0.6,0.7,0.8,0.9, and 1.

- Advertisement -

However, if you want three decimal places, like 0.06, then you need to divide by 100 instead of 10. three will be divided by 1000 in that order.

The code below will generate random numbers from 0 to 1 but it will include three decimal places.

<?php

$randomFloat  = mt_rand(0, 10) / 100;   //the 10 has been changed to 100
echo $randomFloat;

?>

//This is displayed: 0.06

How to generate random floats in PHP code explained

Now let us represent each number with a variable so that you will see it well.

- Advertisement -
<?php

$startingNumber = 0;
$endingNumber = 100;
$decimalPlace = 10;

$randomFloat = mt_rand($startingNumber, $endingNumber) / $decimalPlace;

echo $randomFloat;

?>

Now that we have the variables well represented, let us look at the meaning of each:

$startingNumber: This is the starting number or the least number your random number can be.

$endingNumber: This is the greatest number your random number can be.

$decimalPlace: This indicates the decimal places of your numbers. If it is 10 then your random numbers will be in one decimal place, 100 will be two decimal places, etc. This actually divides the result of the generated random number.

Conclusion

Thank you for reading. We are here to assist you with any of the challenges you will encounter.

Note that neither the rand() nor mt_rand() is good for cryptographic work like password generation or token generation.

- Advertisement -

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

- Advertisement -