I believe everyone is familiar with the rand() function. Many friends have come to this function during the development process. They all know that this function generates PHP random numbers, and the rand() function returns PHP random numbers.INTEGER! Today I will take you to learn more about the rand() function of random number generation in PHP!
First, let’s get to know the random number function rand() provided by php. The rand() function of php will return a random integer. The specific usage is as follows:
rand(min,max)
The optional parameters min and max can be Make rand() return a pseudo-random integer between 0 and rand_max. For example, if you want a random number between 5 and 15 (including 5 and 15), use rand(5, 15).
Below I Let's look at a specific example. We make a basic function call without setting specific parameters. The random number we get will not be limited by the min and max parameters. The code is as follows:
The result obtained :652696728 (Random result) 1. Use PHP to generate random numbers within the specified interval. If we want to generate random numbers between two numbers, we need to set two parameters for rand: In this way, the result we get is in our Under control, it should be minnum
echo(rand(1000,2000));?>
It’s simple enough, Let’s do something a little more difficult. At the beginning of this article, we said that random numbers play a great role. We can use PHP random numbers to solve some complex problems. 2. Using PHP to obtain random elements in a set will set what we need To get a random element from an array, the code is as follows:
$my_array=array('asp','php','网页','ajax','css','jquery','html'); echo($my_array[rand(0,6)]);
It is conceivable that the result we get may be any element contained in the array such as asp, php or javascript. Note , our my_array array contains seven elements, we set the parameters of rand() between 0 and 6, below we use two sets of random numbers to enhance the function of the above example, we need a The random number is used for conditional judgment, and another random number is used as the output of the element. The code is as follows:
$my_array=array('asp','php','javascript','ajax','css','jquery','html'); $repetition=rand(0,6); for($i=0;$i<=$repetition;$i++){ echo('i am learning ' . $my_array[rand(0,6)]); echo(' on 51cto.com'); }
The result we get may be like the following:
First run We got three results. Since we used a random number to limit the number of displayed results, in addition to the random articles, the results we got were also random.
The second run got seven results
Maybe you will ask, can PHP random numbers only do these boring things? rand() does not seem to be that important; you are wrong, think about the verification codes and some cms that can be seen everywhere Systematic random article extraction, download address allocation, etc. Random numbers play an important role in these applications. In addition, in the field of security and algorithms, many applications of random numbers are also worthy of our in-depth study, such as encryption and congruence structures. The code is as follows:
$ip2id= round(rand(600000, 2550000) / 10000); //第一种方法,直接生成 $ip3id= round(rand(600000, 2550000) / 10000); $ip4id= round(rand(600000, 2550000) / 10000); //下面是第二种方法,在以下数据中随机抽取 $arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222", "221","66","59","60","60","66","218","218","62","63","64","66","66","122","211"); $randarr= mt_rand(0,count($arr_1)-1); $ip1id = $arr_1[$randarr]; echo $ip1id; echo "."; echo $ip2id; echo "."; echo $ip3id; echo "."; echo $ip4id; ?>
Summary:
This article is to introduce to you the rand() function for generating random numbers in PHP. For many friends It is a very good choice for us, I hope it will be helpful to your work~
Related recommendations:
The above is the detailed content of rand() function for php random number generation. For more information, please follow other related articles on the PHP Chinese website!

在使用Golang进行开发或学习过程中,我们可能会遇到undefined:rand.Seed的错误提示。这个错误通常会在需要使用随机数生成器时出现,因为在Golang中需要先设置一个随机数种子,才能使用rand包中的函数。本篇文章将介绍如何解决这种错误。1.引入math/rand包首先,我们需要在代码中引入math/rand包。在

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

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

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

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

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

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

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
