Home > Backend Development > PHP Tutorial > PHP method to implement multiple random numbers within a specified range

PHP method to implement multiple random numbers within a specified range

墨辰丷
Release: 2023-03-29 10:32:02
Original
3964 people have browsed it

To generate random data in php, we can use rand, mt_rand can generate random data within a specified range. Let’s introduce the method to students

Call mt_rand()This method Random numbers can be generated, the parameters are the minimum and maximum values ​​of the range, and the function returns a random number between the minimum and maximum values.
To generate truly random numbers is not an easy task for calculation.

There are two methods in PHP to generate random numbers, one is a classic function called rand(), and the other is a more outstanding function is mt_rand().

Example 1

The code is as follows

$random =rand(0,1000);
Copy after login

or

<?php
 $rand = mt_rand(1, 100);
 
 echo $rand;
?>
Copy after login

Example 2

The code is as follows

srand((double)microtime()*1000000); 
$random =rand(0,1000);
Copy after login

Example 3

The code is as follows

/** 
*获取一定范围内的多个随机数字 
*/ 
function yang_numberRand($begin = 0, $end = 20, $limit = 5){ 
  $rand_array = range($begin, $end); 
  shuffle($rand_array); //调用现成的数组随机排列函数 
  return array_slice($rand_array, 0, $limit); //截取前$limit个 
}
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed explanation of PHP thumbnail generation and image watermark production

PHP verification code class ValidateCode analysis

phpUse the preg_match() function to implement the method of verifying the ip address

The above is the detailed content of PHP method to implement multiple random numbers within a specified range. 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