PHP generates multiple random-number instance programs with no duplicates

Source: Internet
Author: User
Tags chr min rand shuffle

The code is as follows:

<?php
Range is to list 1 to 100 as an array
$numbers = range (1,100);
Shuffle the array order immediately
Shuffle ($numbers);
Array_slice a section of the array
$no = 6;
$result = Array_slice ($numbers, 0, $no);
for ($i =0; $i < $no; $i + +) {
echo $result [$i]. " <br> ";
}
Print_r ($result);
?>


Range is to list 1 to 42 as an array
$numbers = range (1,42);
Shuffle the array order immediately
Shuffle ($numbers);
Array_slice a section of the array
$result = Array_slice ($numbers, 0, 3);
Print_r ($result);

Method 2

The code is as follows


<?php
$numbers = range (1,20);
Srand ((float) microtime () *1000000);
Shuffle ($numbers);
while (the list (, $number) = each ($numbers)) {
echo "$number";
}
?>

Method 3

With PHP, in 1-20 randomly generated 5 distinct values, how to do

The code is as follows


<?php
function Norand ($begin =0, $end =20, $limit =5) {
$rand _array=range ($begin, $end);
Shuffle ($rand _array);//Call ready-made array random order function
Return Array_slice ($rand _array,0, $limit);//$limit before intercept
}
Print_r (Norand ());
?>

Or not shuffle.

The code is as follows


<?php
$tmp =array ();
while (count ($tmp) <5) {
$tmp []=mt_rand (1,20);
$tmp =array_unique ($tmp);
}
Print join (', ', $tmp);
?>


It is all on paper, the following is true, the requirements are as follows

There are 25 pieces to vote on, one vote needs to choose 16, and one vote for a single work only once. A programmer in front of the mess, forgot to put the vote into the library, there are 200 users of the voting sequence is empty. So how are you going to fill this mess?

Of course to the superior to reflect the situation. But the technique we're talking about here is the need to generate 1-25 of the 16 random numbers that are not duplicated to fill. Specifically how to design the function? By storing the random number in an array and then removing the duplicate value in the array, a certain number of distinct random numbers can be generated.

The code is as follows


<?php
/*
* Array Unique_rand (int $min, int $max, int $num)
* Generate a certain number of non-repeated random numbers
* $min and $max: specifying the range of random numbers
* $num: Specify the number of builds
*/
function Unique_rand ($min, $max, $num) {
$count = 0;
$return = Array ();
while ($count < $num) {
$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 < count ($arr); $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

Add a few notes:

• The Mt_rand () function is used to generate random numbers. This function generates an average speed of random numbers four times times faster than Rand ().
• Remove duplicate values from the array using the "Flip" method, which is to use Array_flip () to exchange the key and value of the array two times. This approach is much faster than using Array_unique ().

• Before returning the array, use the shuffle () array to give the new key name, ensuring that the key name is a 0-n consecutive number. If you do not do this, you may have trouble with traversal by causing the key name to be discontinuous when you delete duplicate values.


Look at one more example

Generates one of the 36 characters of the 0-z. Each time the GetOptions () method is invoked to generate a single character, they are stored as follows: array[0] = 0, array[1] = 1, ..., array[35] = Z.

The code is as follows

Array (
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
[7] => 7
[8] => 8
[9] => 9
[Ten] => a
[One] => b
[of] => C
[d] =>
[d] => E
[=>] F
[=>] G
[=>] H
[A] => I
[=>] J
[A] => K
[=>] L
[A] => m
[=>] n
[=>] O
[=>] P
[num] => Q
[A] => R
[A] => s
[=>] t
[A] => u
[To] => V
[A] => W
[=>] X
[=>] Y
[km] => Z
)

Then randomly generate a number between 0-35 as an index, in fact, in the above array randomly fetched a number, as the variable $result the first character. This random index is then assigned the last of the array, and it will not participate in the next round of random selection.

The code is as follows


<?php
Generate one character in 0123456789abcdefghijklmnopqrstuvwxyz
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 realize random number
For ($j =0 $j <100; $j + +)
{
$result = "";
$options = GetOptions ();
$lastIndex = 35;
while (strlen ($result) < $len)
{
Randomly take one from 0 to 35 as an index
$index = rand (0, $lastIndex);
Assign a random number to a variable $CHR
$CHR = $options [$index];
Random numbers as part of the $result
$result. = $CHR;
$lastIndex = $lastIndex-1;
The last index will not participate in the next random draw
$options [$index] = $options [$lastIndex];
}
echo $result. " n ";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.