search
Homephp教程php手册PHP也能干大事 随机函数

这篇文章主要介绍了PHP也能干大事 随机函数,需要的朋友可以参考下

写在前面

PHP也能干大事是我总结的PHP语法特性及相关函数类库的经典用法,并不一定是真正能实现四两拨千斤的功效,但是掌握这些方法,可以在你的工作和学习上有一些帮助,希望大家能集思广益,将《PHP也能干大事》丰富得更精彩!转载请注明出处(3mc2.com)

二、前言

PHP是常见的脚本语言,主要是因为其简单易学,上手快,几乎50%以上的Web程序都有PHP的身影(不完全统计)。PHP为开发这提供了丰富的函数和API接口,这使得我们能够非常方便地使用其强大的内置函数及扩展,本文是《PHP也能干大事》系列的第二篇,主要总结PHP在随机数生成器方面的知识。

三、PHP随机函数

PHP随机函数主要有rand、mt_rand、array_rand,还有随机“排列”(打乱顺序)的函数shuffle、str_shuffle,能够产生唯一ID的uniqid。

1、rand产生随机数:

rand()函数是使用libc 的随机数发生器生成随机数的,一般较慢,且有不确定因素,建议使用mt_rand函数替代。

getrandmax()函数可以返回rand函数能够产生的最大的随机数(我的系统为32767),所以在设置rand函数第二个参数时不要超高getrandmax的返回值。

2、mt_rand产生随机数:

很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。mt_rand() 函数是非正式用来替换它的。该函数用了 » Mersenne Twister 中已知的特性作为随机数发生器,它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。强烈建议在开发过程中使用mt_rand函数取代rand。

如果没有提供可选参数 min 和 max,mt_rand() 返回 0 到 mt_getrandmax() 之间的伪随机数。例如想要 3到 20(包括 3 和 20)之间的随机数,用 mt_rand(3, 20)。

3、array_rand函数

array_rand返回数组中随机的键值,有点类似mt_rand()函数,其余的没什么特殊的地方,灵活运用而已。

4、shuffle函数

shuffle函数是打乱一个数组的序列,有点随机的意思,这里就放在随机函数里了。返回值为bool值,相当于直接引用了原变量。

5、str_shuffle函数

这里str_shuffle的功能和shuffle的功能是类似的,唯一不同的是返回值,str_shuffle的原字符串是不变的。

6、uniqid函数

uniqid能够产生唯一的字符串,这个应用的范围可以相当广泛

四、总结
随机函数几乎是每种语言最基本的功能,PHP对于随机函数的支持也不例外,,这里介绍了rand、mt_rand、array_rand、shuffle、str_shuffle、uniqid函数的基本用法,更多可以结合实际应用灵活运用。

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

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment