PHP Mathematical Function Practice 5: Get Random Floating Point Numbers

藏色散人
Release: 2023-03-12 08:10:01
Original
2274 people have browsed it

Today we will continue to bring you the content of the PHP mathematical function practice series, so in the previous article "PHP mathematical function practice four: round floating point numbers from zero to the specified number of decimal places 》Introduces you how to round floating point numbers from zero to a specified number of decimal places. Friends in need can learn about it~

The theme of this article is to teach you how to write a PHP function to get a random floating point number.

First of all, I will give you a brief introduction to what floating point numbers are. Floating point numbers are also called double precision numbers or real numbers. They can be defined in PHP using any of the following syntax:

<?php
$a = 1.234; 
$b = 1.2e3; 
$c = 7E-10;
$d = 1_234.567; // 从 PHP 7.4.0 开始支持
?>
Copy after login

I will explain below Directly introduce the implementation method of obtaining random floating point numbers:

The PHP code example is as follows:

<?php
function rand_float($st_num=0,$end_num=1,$mul=1000000)
{
    if ($st_num>$end_num) return false;
    return mt_rand($st_num*$mul,$end_num*$mul)/$mul;
}
echo rand_float()."<br>";
echo rand_float(0.6)."<br>";
echo rand_float(0.5,0.6)."<br>";
echo rand_float(0,20)."<br>";
echo rand_float(0,3,2)."<br>";
echo rand_float(0,2,20)."<br>";
?>
Copy after login

The output result is:

0.353804
0.715058
0.598421
13.484704
0.5
1.4
Copy after login

The refresh operation effect is as follows:

GIF 2021-8-26 星期四 上午 9-11-35.gif

#As shown above, we can get random floating point numbers.

Here we introduce a functionmt_randFunction:

mt_rand() function uses the Mersenne Twister algorithm to generate random integers.

This function is a better choice for generating random values, returning results 4 times faster than the rand() function; if you want a random value between 10 and 100 (inclusive) For integers, use mt_rand (10,100).

The syntax is:

mt_rand();
or
mt_rand(min,max);
Copy after login

The return value is a random integer between min (or 0) and max (or mt_getrandmax()) (including boundary values). If max < min returns FALSE.

注:浮点数的精度
浮点数的精度有限。尽管取决于系统,PHP 通常使用 IEEE 754 双精度格式,则由于取整而导致的最大相对误差为 1.11e-16。非基本数学运算可能会给出更大误差,并且要考虑到进行复合运算时的误差传递。
此外,以十进制能够精确表示的有理数如 0.1 或 0.7,无论有多少尾数都不能被内部所使用的二进制精确表示,因此不能在不丢失一点点精度的情况下转换为二进制的格式。这就会造成混乱的结果:例如,floor((0.1+0.7)*10) 通常会返回 7 而不是预期中的 8,因为该结果内部的表示其实是类似 7.9999999999999991118...。
所以永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。如果确实需要更高的精度,应该使用任意精度数学函数或者 gmp 函数。
Copy after login

PHP Chinese website platform has a lot of video teaching resources. Welcome everyone to learn "PHP Video Tutorial"!

The above is the detailed content of PHP Mathematical Function Practice 5: Get Random Floating Point Numbers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template