search
HomeBackend DevelopmentPHP TutorialHow to generate random numbers in php
How to generate random numbers in phpNov 10, 2017 pm 02:05 PM
phpgeneraterandom number

I believe that when friends see the title, their first reaction is Use function to implement PHP random numbers. So what functions are there to implement PHP random numbers? So today we will introduce to you what methods are there for generating random numbers in PHP?

How to use php to generate non-repeating random numbers between 1-10?

1. Use the shuffle function to generate php random numbers.

<?php
$arr=range(1,10);
shuffle($arr);
foreach($arr as $values)
{
 echo $values." ";
}
?>

2, use the array_unique function to generate php random numbers.

<?php
$arr=array();
while(count($arr)<10)
{
 $arr[]=rand(1,10);
 $arr=array_unique($arr);
}
echo implode(" ",$arr);
?>

Example 3, use the array_flip function to generate PHP random numbers, which can remove duplicate values.

<?php
$arr=array();
$count1=0;
$count = 0;
$return = array();
while ($count < 10) 
 {
 $return[] = mt_rand(1, 10);
 $return = array_flip(array_flip($return));
 $count = count($return);
 } //www.jb51.net
foreach($return as $value)
 {
 echo $value." ";
 }
echo "<br/>";
$arr=array_values($return);// 获得数组的值 
foreach($arr as $key)
echo $key." ";
?>

php Random number generation function example

<?php
function randpw($len=8,$format=&#39;ALL&#39;){
$is_abc = $is_numer = 0;
$password = $tmp =&#39;&#39;; 
switch($format){
case &#39;ALL&#39;:
$chars=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&#39;;
break;
case &#39;CHAR&#39;:
$chars=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&#39;;
break;
case &#39;NUMBER&#39;:
$chars=&#39;0123456789&#39;;
break;
default :
$chars=&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&#39;;
break;
} // www.jb51.net
mt_srand((double)microtime()*1000000*getmypid());
while(strlen($password)<$len){
$tmp =substr($chars,(mt_rand()%strlen($chars)),1);
if(($is_numer <> 1 && is_numeric($tmp) && $tmp > 0 )|| $format == &#39;CHAR&#39;){
$is_numer = 1;
}
if(($is_abc <> 1 && preg_match(&#39;/[a-zA-Z]/&#39;,$tmp)) || $format == &#39;NUMBER&#39;){
$is_abc = 1;
}
$password.= $tmp;
}
if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){
$password = randpw($len,$format);
}
return $password;
}
for($i = 0 ; $i < 10; $i++){
echo randpw(8,&#39;NUMBER&#39;);
echo "<br>";
}

Summary:

php There are many ways to generate random numbers, and each method has its own characteristics and effects. So in our daily development, we only need to choose a certain method according to our own business needs!

Related recommendations:

PHP random number

##Several methods of generating PHP random numbers

php random number WeChat red envelope random generation algorithm php version


The above is the detailed content of How to generate random numbers in php. 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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

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

如何在 Excel 中创建随机数生成器如何在 Excel 中创建随机数生成器Apr 14, 2023 am 09:46 AM

如何使用 RANDBETWEEN 在 Excel 中生成随机数如果要生成特定范围内的随机数,RANDBETWEEN 函数是一种快速简便的方法。这允许您在您选择的任何两个值之间生成随机整数。使用 RANDBETWEEN 在 Excel 中生成随机数:单击您希望出现第一个随机数的单元格。键入=RANDBETWEEN(1,500)将“1”替换为您要生成的最低随机数,将“500”替换为

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

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

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

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

Java随机数生成性能优化方法Java随机数生成性能优化方法Jun 30, 2023 pm 12:25 PM

如何优化Java开发中的随机数生成性能随机数在计算机科学中有广泛的应用,特别是在密码学、模拟、游戏等领域。在Java开发中,我们常常需要生成随机数来满足各种需求。然而,随机数生成的性能通常是开发者关注的问题之一。本文将探讨如何优化Java开发中的随机数生成性能。使用ThreadLocalRandom类在Java7中引入了ThreadLocalRandom类

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool