Home > Backend Development > PHP Problem > Two ways to generate random numbers in php

Two ways to generate random numbers in php

autoload
Release: 2023-03-09 13:46:01
Original
2881 people have browsed it

Two ways to generate random numbers in php

In order to create some contingency in php, we may need to set the values ​​​​of some variables to random. In order to solve this problem, php has built-in functions rand() and mt_rand() functions to solve this problem.

First let’s take a look at the syntax of the mt_rand() function:

mt_rand    ( int $min   , int $max   )
Copy after login
  • $min: Optional, the minimum value of the generated number

  • $max: Optional, the maximum value of the generated number

  • Return value: If there is no minimum value and maximum value, a random value will be returned The maximum possible value of a number, if any, between the minimum and maximum values.

Code example:

<?php
echo mt_rand();
echo "<br>";
echo mt_rand(10,100);
?>
Copy after login
输出:
1144620466 //随机不一定相同
12         //随机不一定相同
Copy after login

Note: mt_rand()generates random values ​​faster on average than rand() Four times.

Recommended: 2021 PHP interview questions summary (collection)》《php video Tutorial

The above is the detailed content of Two ways to generate random numbers in php. 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