


PHP generates multiple non-repeating random numbers example program_PHP tutorial
Generating random data in php can be achieved directly by using mt_rand. If we want to generate non-repeating random numbers, we can use the unique_rand function. Let me summarize the commonly used methods.
The code is as follows:
The code is as follows | Copy code | ||||||||
$numbers = range (1,100); //shuffle will disrupt the order of the array shuffle ($numbers); //array_slice takes a certain segment of the array $no=6; $result = array_slice($numbers,0,$no); for ($i=0;$i<$no;$i++){ echo $result[$i]." "; } print_r($result); ?>
//range is to list 1 to 42 into an array $numbers = range (1,42); //shuffle will disrupt the order of the array shuffle ($numbers); //array_slice takes a certain segment of the array $result = array_slice($numbers,0,3); print_r($result);
|
Method 2
The code is as follows | Copy code |
$numbers = range (1,20); |
代码如下 | 复制代码 |
function NoRand($begin=0,$end=20,$limit=5){ |
Use PHP to randomly generate 5 unique values between 1-20. How to do it
代码如下 | 复制代码 |
$tmp=array(); |
The code is as follows | Copy code |
function NoRand($begin=0,$end=20,$limit=5){ $rand_array=range($begin,$end); shuffle($rand_array);//Call the ready-made array random arrangement function return array_slice($rand_array,0,$limit); //Intercept the first $limit items } print_r(NoRand()); ?> |
The code is as follows | Copy code |
$tmp=array(); while(count($tmp)<5){ $tmp[]=mt_rand(1,20); $tmp=array_unique($tmp); } print join(',',$tmp); ?> |
The above is all talk on paper, now comes the reality. The requirements are as follows
There are 25 works for voting. You need to select 16 works in one vote. A single work can only be selected once in one vote. A programmer made a mistake earlier and forgot to store the votes in the database. The voting sequences generated by 200 users were empty. So how do you fill this gap?
Of course, report the situation to your superiors. But what we are discussing here is technology, which requires generating 16 non-repeating random numbers between 1-25 to fill in. How to design the function specifically? Store random numbers in an array, and then remove duplicate values in the array to generate a certain number of non-repeating random numbers
The code is as follows | Copy code | ||||||||||||||||||||
* $num: Specify the generated quantity $count = 0; $return = array();
While ($count < $num) { Shuffle($return); Return $return;
Then randomly generate a number between 0-35 as the index, which is actually to randomly pick out a number from the above array as the first character in the variable $result. This random index will then be assigned as the last one in the array, and it will not participate in the next round of random selection.
function getOptions() { $options = array(); $result = array(); for($i=48; $i<=57; $i++) { array_push($options,chr($i)); } for($i=65; $i<=90; $i++) { $j = 32; $small = $i + $j; array_push($options,chr($small)); } return $options; } /* $e = getOptions(); for($j=0; $j<150; $j++) { echo $e[$j]; } */ $len = 10; // Randomly generate array index to achieve random numbers for($j=0; $j<100; $j++) { $result = ""; $options = getOptions(); $lastIndex = 35; while (strlen($result)<$len) { // Randomly pick one from 0 to 35 as the index $index = rand(0,$lastIndex); //Assign random number to variable $chr $chr = $options[$index]; // Random number as part of $result $result .= $chr; $lastIndex = $lastIndex-1; //The last index will not participate in the next random draw $options[$index] = $options[$lastIndex]; } echo $result."n"; } ?> http: //www.bkjia.com/PHPjc/633165.html TechArticle Generating random data in php can be achieved directly using mt_rand. If we want to generate non-repeating random numbers, we can use unique_rand function, let me summarize the commonly used methods. ...
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
![]() Hot AI Tools![]() Undresser.AI UndressAI-powered app for creating realistic nude photos ![]() AI Clothes RemoverOnline AI tool for removing clothes from photos. ![]() Undress AI ToolUndress images for free ![]() Clothoff.ioAI clothes remover ![]() Video Face SwapSwap faces in any video effortlessly with our completely free AI face swap tool! ![]() Hot Article
What's New in Windows 11 KB5054979 & How to Fix Update Issues
3 weeks ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
2 weeks ago
By DDD
InZoi: How To Apply To School And University
3 weeks ago
By DDD
How to fix KB5055518 fails to install in Windows 10?
2 weeks ago
By DDD
Roblox: Dead Rails – How To Summon And Defeat Nikola Tesla
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
![]() Hot Tools![]() Notepad++7.3.1Easy-to-use and free code editor ![]() SublimeText3 Chinese versionChinese version, very easy to use ![]() Zend Studio 13.0.1Powerful PHP integrated development environment ![]() Dreamweaver CS6Visual web development tools ![]() SublimeText3 Mac versionGod-level code editing software (SublimeText3) ![]() Hot Topics![]() PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati ![]() If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op ![]() Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c ![]() JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably, ![]() A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total ![]() This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an ![]() Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead. ![]() What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency. ![]() |