Php generates multiple random number instances.

Source: Internet
Author: User
Php generates multiple random number instances. You can directly use mt_rand to generate random data in php. if you want to generate random numbers without duplicates, you can use the unique_rand function. below I will summarize the commonly used methods. You can directly use mt_rand to generate random data in php. if you want to generate random numbers without duplicates, you can use the unique_rand function. below I will summarize the commonly used methods.

The code is as follows:

The code is as follows:

// Range is an array of columns from 1 to 100.
$ Numbers = range (1, 1,100 );
// Shuffle will immediately disrupt the array order
Shuffle ($ numbers );
// Array_slice retrieves a 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 an array of columns from 1 to 42.
$ Numbers = range (1, 42 );
// Shuffle will immediately disrupt the array order
Shuffle ($ numbers );
// Array_slice retrieves a segment of the array
$ Result = array_slice ($ numbers, 0, 3 );
Print_r ($ result );

Method 2

The code is as follows:

$ Numbers = range (1, 20 );
Srand (float) microtime () * 1000000 );
Shuffle ($ numbers );
While (list (, $ number) = each ($ numbers )){
Echo "$ number ";
}
?>

Method 3

With PHP, 5 non-repeated values are randomly generated between 1 and 20. how can this problem be solved?

The code is as follows:

Function NoRand ($ begin = 0, $ end = 20, $ limit = 5 ){
$ Rand_array = range ($ begin, $ end );
Shuffle ($ rand_array); // call the random array arrangement function.
Return array_slice ($ rand_array, 0, $ limit); // The first $ limit
}
Print_r (NoRand ());
?>

Or do not shuffle

The code is as follows:

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

The above are all on paper, and the following are true: the requirements are as follows:

You can vote for 25 pieces of data. you can only vote for 16 pieces of data at a time. The previous programmer threw a loophole and forgot to put the voting into the database. the voting sequence generated by 200 users was empty. So how do you fill in this leak?

Of course, reflect the situation to the superior. However, we will discuss the technology here, that is, we need to generate 16 non-repeated random numbers between 1 and 25 to fill. How to design functions? Store the random number into the array, and then remove the repeated values in the array to generate a certain number of non-repeated random numbers.

The code is as follows:

/*
* Array unique_rand (int $ min, int $ max, int $ num)
* Generate a certain number of non-repeated random numbers
* $ Min and $ max: specify the random number range.
* $ Num: specifies the number of instances generated.
*/
Function unique_rand ($ min, $ max, $ num ){
$ Count = 0;
$ Return = array ();
While ($ count <$ num ){
$ Return [] = mt_rand ($ min, $ max );
$ Return = 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

Additional notes:

• The mt_rand () function is used to generate random numbers. The average speed of this function to generate random numbers is four times faster than rand.
• The "flip method" is used to remove duplicate values in the array, that is, the key and value of the array are exchanged twice with array_flip. This approach is much faster than using array_unique.

• Before returning an array, use shuffle () to assign a new key name to the array to ensure that the key name is a continuous number ranging from 0 to n. If you do not perform this step, the key name may not be consecutive when you delete duplicate values, which may cause trouble for traversal.


Let's look at another instance.

Generate one of the 36 characters 0-Z. Each time you call the getOptions () method to generate a 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
[10] =>
[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 a random number is generated between 0 and 35 as the index. In fact, a random number is taken out of the above array as the first character in the variable $ result. This random index will be assigned to the last one of the array and will not participate in the next round of random selection.

The code is as follows:

// Generate a 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 an array index to achieve a random number
For ($ j = 0; $ j <100; $ j ++)
{
$ Result = "";
$ Options = getOptions ();
$ LastIndex = 35;
While (strlen ($ result) <$ len)
{
// Random index from 0 to 35
$ Index = rand (0, $ lastIndex );
// Assign the random number to the variable $ chr
$ Chr = $ options [$ index];
// Random number as part of $ result
$ Result. = $ chr;
$ LastIndex = $ lastIndex-1;
// The last index will not be used in the next random lottery.
$ Options [$ index] = $ options [$ lastIndex];
}
Echo $ result. "n ";
}
?>

Using the unique_rand function, I will summarize the common methods below ....

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.