Home  >  Article  >  Backend Development  >  What is the usage of php rand function?

What is the usage of php rand function?

藏色散人
藏色散人Original
2021-03-11 10:02:034281browse

php The rand function is used to return a random integer. The syntax for using this function is "rand(min,max)", where the parameters "min" and "max" specify the range of random numbers. If not provided, Select the parameters min and max, and the rand function returns a pseudo-random integer between 0 and RAND_MAX.

What is the usage of php rand function?

The operating environment of this article: windows7 system, PHP8 version, DELL G3 computer

rand() function returns a random integer.

Syntax

rand(min,max)

Parameters

  • min,max are optional. Specifies the range of random number generation.

Description

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

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

  • 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 rand (5, 11).

Example

This example will return some random numbers:

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

Output:

What is the usage of php rand function?

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of What is the usage of php 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