search
HomeBackend DevelopmentPHP TutorialComparison of the differences between the php rand() function and the mt_rand() function

mt_rand() is four times faster than rand(). Many old libc random number generators have some uncertain and unknown characteristics and are very slow. PHP's rand() Function uses the libc random number generator by default. The mt_rand() function is informally used to replace it.

PHP functionsrand and mt_rand
 
mt_rand() is four times faster than rand()
 
Many old libc random number generators have some Uncertain and unknown properties and very slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features in Mersenne Twister as a random number generator. mt_rand() can generate random values ​​​​on average four times faster than rand() provided by libc.
 
mt_rand() is four times faster than rand()
 
 mt_rand — generates better random numbers
 
  (PHP 3 >= 3.0.6, PHP 4, PHP 5)
 
  int mt_rand ( [int min, int max] )
 
  Many old libc random number generators have some uncertain and unknown characteristics and are very slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known properties of Mersenne Twister as a random number generator, which can generate random values ​​on average four times faster than rand() provided by libc.
 
  If the optional parameters min and max are not provided, mt_rand() returns
 
  A pseudo-random number between 0 and RAND_MAX.
 
 For example, if you want a random number between 5 and 15 (inclusive), use mt_rand(5, 15).
 
Note: Since PHP 4.2.0, it is no longer necessary to use the srand() or mt_srand() function to seed the random number generator, it is now completed automatically .
 
 rand — Generate a random integer
  (PHP 3, PHP 4, PHP 5)
 
 int rand ([int min, int max] )
 
 If the optional parameters min and max are not provided, rand() returns a pseudo-random integer between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (inclusive), use rand(5, 15).
 
Note: On some platforms (such as Windows) RAND_MAX is only 32768. If you need a range greater than 32768, specify the min and max parameters to generate a number greater than RAND_MAX, or consider using mt_rand() instead.
 
Note: As of PHP 4.2.0, it is no longer necessary to use the srand() or mt_srand() function to seed the random number generator, it is now done automatically.


mt_rand definition and usage
mt_rand() returns a random integer using the Mersenne Twister algorithm.

Syntax
mt_rand(min,max) Description
If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random number between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (inclusive), use mt_rand(5, 15).

Many old libc random number generators have some uncertain and unknown characteristics and are slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of Mersenne Twister as a random number generator, which can generate random values ​​on average four times faster than rand() provided by libc.
Tips and
Notes Note: As of PHP 4.2.0, seeding the random number generator with the srand() or mt_srand() functions is no longer necessary, it is now done automatically.

Note: In versions before 3.0.7, max means range. To get the same random numbers from 5 to 15 as in the above example in these versions, the short example is mt_rand (5, 11).
Example

In this example, we will return some random numbers:

The code is as follows:

<?php 
echo(mt_rand()); 
echo(mt_rand()); 
echo(mt_rand(10,100)); 
?>

The output is similar:


3150906288
513289678
35

The above is the detailed content of Comparison of the differences between the php rand() function and the mt_rand() function. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
如何解决 golang 中的 “undefined: rand.Seed” 错误?如何解决 golang 中的 “undefined: rand.Seed” 错误?Jun 25, 2023 am 08:34 AM

在使用Golang进行开发或学习过程中,我们可能会遇到undefined:rand.Seed的错误提示。这个错误通常会在需要使用随机数生成器时出现,因为在Golang中需要先设置一个随机数种子,才能使用rand包中的函数。本篇文章将介绍如何解决这种错误。1.引入math/rand包首先,我们需要在代码中引入math/rand包。在

php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.