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. the instance code is as follows: & lt ;? Phprange is
You can directly use mt_rand to generate random data in php. to generate random numbers without duplicates, you can use the unique_rand function. the instance 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 );
The example code is as follows:
-
- $ Numbers = range (1, 20 );
- Srand (float) microtime () * 1000000 );
- Shuffle ($ numbers );
- While (list (, $ number) = each ($ numbers )){
- Echo "$ number ";
- }
- ?>
Instance Code 3,With PHP, 5 non-repeated values are randomly generated between 1 and 20. 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 is a piece of paper, and the following is true. the requirements are as follows:
There are 25 pieces of work to vote, one vote requires 16, a single piece of work can only be selected once, the previous programmer stabbed a leak, forgot to put the vote into the database, how do you fill in the empty voting sequence generated by 200 users?
Of course, we will reflect the situation to our superiors, but here we will discuss the technology, that is, we need to generate 16 non-repeated random numbers between 1 and 25 to fill in. how to design a function? 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, 2, 3, 6, 7, 8, 9, 10, 11, 12, 13, 16, 20, 21
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. it 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 ";
- }
- ?>