PHP函数:生成N个不重复的随机数,php 随机数
PHP函数:生成N个不重复的随机数
思路:将生成的随机数存入数组,再在数组中去除重复的值,即可生成一定数量的不重复随机数。
程序:
<?php /* * array unique_rand( int $min, int $max, int $num ) * 生成一定数量的不重复随机数 * $min 和 $max: 指定随机数的范围 * $num: 指定生成数量 */ function unique_rand($min,$max,$num){ $count = 0; $return_arr = array(); while($count < $num){ $return_arr[] = mt_rand($min,$max); $return_arr = array_flip(array_flip($return_arr)); $count = count($return_arr); } shuffle($return_arr); return $return_arr; }
补充说明:
1、生成随机数使用了mt_rand()函数,这个函数比rand()函数快4倍;
2、去除数组重复值时采用了“翻翻法”,就是用array_flip()把数组的key和value交换两次。比用array_unique()快很多。
rand(1,8)*100
ans =
Columns 1 through 7
81.4724 90.5792 12.6987 91.3376 63.2359 9.7540 27.8498
Column 8
54.6882
选中A1:A1000,在编辑栏输入
=RAND()
按【CTRL+回车】
再选中B1:B1000,在编辑栏输入
=RANK(A1,a$1:A$1000)
按【CTRL+回车】

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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