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. ...
Related labels:
source:php.cn
Previous article:phpmyadmin error: Cannot start session without errors_PHP Tutorial
Next article:PHP strtotime calculation of today's problem in the previous month - PHP tutorial
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
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|