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 |
代码如下 |
复制代码 |
//range 是将1到100 列成一个数组
$numbers = range (1,100);
//shuffle 将数组顺序随即打乱
shuffle ($numbers);
//array_slice 取该数组中的某一段
$no=6;
$result = array_slice($numbers,0,$no);
for ($i=0;$i
echo $result[$i]." ";
}
print_r($result);
?>
//range 是将1到42 列成一个数组
$numbers = range (1,42);
//shuffle 将数组顺序随即打乱
shuffle ($numbers);
//array_slice 取该数组中的某一段
$result = array_slice($numbers,0,3);
print_r($result);
|
//range is to list 1 to 100 into an array
$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
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);
代码如下 |
复制代码 |
$numbers = range (1,20);
srand ((float)microtime()*1000000);
shuffle ($numbers);
while (list (, $number) = each ($numbers)) {
echo "$number ";
}
?>
|
|
Method 2
The code is as follows |
Copy code |
$numbers = range (1,20);
srand ((float)microtime()*1000000);
shuffle ($numbers);
while (list (, $number) = each ($numbers)) {
echo "$number ";
}
?>
|
代码如下 |
复制代码 |
function NoRand($begin=0,$end=20,$limit=5){
$rand_array=range($begin,$end);
shuffle($rand_array);//调用现成的数组随机排列函数
return array_slice($rand_array,0,$limit);//截取前$limit个
}
print_r(NoRand());
?>
|
Method 3
Use PHP to randomly generate 5 unique values between 1-20. How to do it
代码如下 |
复制代码 |
$tmp=array();
while(count($tmp)
$tmp[]=mt_rand(1,20);
$tmp=array_unique($tmp);
}
print join(',',$tmp);
?>
|
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());
?>
|
Or if you don’t shuffle
The code is as follows |
Copy code |
$tmp=array();
while(count($tmp)
$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 |
代码如下 |
复制代码 |
/*
* array unique_rand( int $min, int $max, int $num )
* 生成一定数量的不重复随机数
* $min 和 $max: 指定随机数的范围
* $num: 指定生成数量
*/
function unique_rand($min, $max, $num) {
$count = 0;
$return = array();
while ($count
$return[] = mt_rand($min, $max);
$return = array_flip(array_flip($return));
$count = count($return);
}
shuffle($return);
return $return;
}
$arr = unique_rand(1, 25, 16);
sort($arr);
$result = '';
for($i=0; $i
{
$result .= $arr[$i].',';
}
$result = substr($result, 0, -1);
echo $result;
?>
程序运行如下:
1 2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24
|
/*
* array unique_rand( int $min, int $max, int $num )
* Generate a certain number of non-repeating random numbers
* $min and $max: specify the range of random numbers
* $num: Specify the generated quantity
*/
function unique_rand($min, $max, $num) {
$count = 0;
$return = array();
While ($count
$return[] = mt_rand($min, $max);
$return = array_flip(array_flip($return));
$count = count($return);
}
Shuffle($return);
Return $return;
代码如下 |
复制代码 |
Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => a
[11] => b
[12] => c
[13] => d
[14] => e
[15] => f
[16] => g
[17] => h
[18] => i
[19] => j
[20] => k
[21] => l
[22] => m
[23] => n
[24] => o
[25] => p
[26] => q
[27] => r
[28] => s
[29] => t
[30] => u
[31] => v
[32] => w
[33] => x
[34] => y
[35] => z
)
|
}
$arr = unique_rand(1, 25, 16);
sort($arr);
$result = '';
for($i=0; $i
{
$result .= $arr[$i].',';
}
$result = substr($result, 0, -1);
echo $result;
?>
The program runs as follows:
1 2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24
|
A few additional notes:
•The mt_rand() function is used to generate random numbers. This function generates random numbers four times faster on average than rand().
•When removing duplicate values from an array, the "flip method" is used, which is to use array_flip() to exchange the key and value of the array twice. This approach is much faster than using array_unique().
•Before returning the array, first use shuffle() to assign new key names to the array, ensuring that the key names are consecutive numbers from 0-n. If this step is not performed, the key names may be discontinuous when deleting duplicate values, causing trouble in traversal.
Look at another example
Generate one of the 36 characters 0-z. Each call to the getOptions() method generates a character, which is stored as follows: array[0] = 0, array[1] = 1, …, array[35] = z.
The code is as follows |
Copy code |
Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[10] => a
[11] => b
[12] => c
[13] => d
[14] => e
[15] => f
[16] => g
[17] => h
[18] => i
[19] => j
[20] => k
[21] => l
[22] => m
[23] => n
[24] => o
[25] => p
[26] => q
[27] => r
[28] => s
[29] => t
[30] => u
[31] => v
[32] => w
[33] => x
[34] => y
[35] => z
)
|
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.
The code is as follows
代码如下 |
复制代码 |
// 生成0123456789abcdefghijklmnopqrstuvwxyz中的一个字符
function getOptions()
{
$options = array();
$result = array();
for($i=48; $i
{
array_push($options,chr($i));
}
for($i=65; $i
{
$j = 32;
$small = $i + $j;
array_push($options,chr($small));
}
return $options;
}
/*
$e = getOptions();
for($j=0; $j
{
echo $e[$j];
}
*/
$len = 10;
// 随机生成数组索引,从而实现随机数
for($j=0; $j
{
$result = "";
$options = getOptions();
$lastIndex = 35;
while (strlen($result)
{
// 从0到35中随机取一个作为索引
$index = rand(0,$lastIndex);
// 将随机数赋给变量 $chr
$chr = $options[$index];
// 随机数作为 $result 的一部分
$result .= $chr;
$lastIndex = $lastIndex-1;
// 最后一个索引将不会参与下一次随机抽奖
$options[$index] = $options[$lastIndex];
}
echo $result."n";
}
?>
|
|
Copy code |
|
// Generate a character in 0123456789abcdefghijklmnopqrstuvwxyz
function getOptions()
{
$options = array();
$result = array();
for($i=48; $i
{
array_push($options,chr($i));
}
for($i=65; $i
{
$j = 32;
$small = $i + $j;
array_push($options,chr($small));
}
return $options;
}
/*
$e = getOptions();
for($j=0; $j
{
echo $e[$j];
}
*/
$len = 10;
// Randomly generate array index to achieve random numbers
for($j=0; $j
{
$result = "";
$options = getOptions();
$lastIndex = 35;
while (strlen($result)
{
// 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.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/633165.htmlGenerating 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. ...