fbpx
  • Home
  • >>
  • Blog
  • >>
  • Tech
  • >>
  • How to Get Random Float in PHP – Best Guide
Sharing Is Caring!

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.

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.

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.

<?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.

Overall Ratings
Click to rate this post!
[Total: 0 Average: 0]

Sharing Is Caring!
Leave a Reply
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Latest Articles

StreetWayz

Sharing Is Caring! This post you guide you on how to program

StreetWayz

Sharing Is Caring! If you are searching for how to delete Five

StreetWayz

Sharing Is Caring! The internet has become a necessary component of our

StreetWayz

Sharing Is Caring! Menulog has been one of the most popular food-based

StreetWayz

Sharing Is Caring! This post will look at how to set up

StreetWayz

Sharing Is Caring! In this article, we will look at how to

Free Download

Guide: How to Get [Benefit] Without [Pain Point]

How to Get (benefit) Without (pain point)