How to use rand() function in php

下次还敢
Release: 2024-04-29 10:57:12
Original
684 people have browsed it

The rand() function in PHP is used to generate random integers ranging from 0 to RAND_MAX (default 2147483647). You can specify the minimum and maximum values ​​as parameters, and the function returns a random integer between the two values.

How to use rand() function in php

Usage of rand() function in PHP

rand() function is a built-in function for Generate random numbers. It generates a random integer ranging from 0 to RAND_MAX, which is a constant and defaults to 2147483647.

Syntax:

<code class="php">int rand([int $min [, int $max]])</code>
Copy after login

Parameters:

  • $min: (optional ) specifies the minimum value of the random number, the default is 0.
  • $max: (optional) Specifies the maximum value of the random number, the default is RAND_MAX.

Return value:

The function returns a random integer between $min and $max (inclusive of bounds).

Example:

<code class="php">// 生成一个随机数(0-2147483647)
$num = rand();

// 生成一个随机数(10-100)
$num = rand(10, 100);</code>
Copy after login

Note:

  • rand() function can only generate pseudo-random numbers. The randomness on depends on the input (seed).
  • If the rand() function is called multiple times in the same script, predictable random numbers will be generated. To get truly random numbers, you need to use the mt_rand() or random_int() function.

The above is the detailed content of How to use rand() function in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!