Php generates multiple random number instances

Source: Internet
Author: User
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:

  1. // Range is an array of columns from 1 to 100.
  2. $ Numbers = range (1, 1,100 );
  3. // Shuffle will immediately disrupt the array order
  4. Shuffle ($ numbers );
  5. // Array_slice retrieves a segment of the array
  6. $ No = 6;
  7. $ Result = array_slice ($ numbers, 0, $ no );
  8. For ($ I = 0; $ I <$ no; $ I ++ ){
  9. Echo $ result [$ I]."
    ";
  10. }
  11. Print_r ($ result );
  12. ?>
  13. // Range is an array of columns from 1 to 42.
  14. $ Numbers = range (1, 42 );
  15. // Shuffle will immediately disrupt the array order
  16. Shuffle ($ numbers );
  17. // Array_slice retrieves a segment of the array
  18. $ Result = array_slice ($ numbers, 0, 3 );
  19. Print_r ($ result );

The example code is as follows:

  1. $ Numbers = range (1, 20 );
  2. Srand (float) microtime () * 1000000 );
  3. Shuffle ($ numbers );
  4. While (list (, $ number) = each ($ numbers )){
  5. Echo "$ number ";
  6. }
  7. ?>

Instance Code 3,With PHP, 5 non-repeated values are randomly generated between 1 and 20. the code is as follows:

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

Or do not shuffle, the code is as follows:

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

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:

  1. /*
  2. * Array unique_rand (int $ min, int $ max, int $ num)
  3. * Generate a certain number of non-repeated random numbers
  4. * $ Min and $ max: specify the random number range.
  5. * $ Num: specifies the number of instances generated.
  6. */
  7. Function unique_rand ($ min, $ max, $ num ){
  8. $ Count = 0;
  9. $ Return = array ();
  10. While ($ count <$ num ){
  11. $ Return [] = mt_rand ($ min, $ max );
  12. $ Return = array_flip ($ return ));
  13. $ Count = count ($ return );
  14. }
  15. Shuffle ($ return );
  16. Return $ return;
  17. }
  18. $ Arr = unique_rand (1, 25, 16 );
  19. Sort ($ arr );
  20. $ Result = '';
  21. For ($ I = 0; $ I <count ($ arr); $ I ++)
  22. {
  23. $ Result. = $ arr [$ I]. ',';
  24. }
  25. $ Result = substr ($ result, 0,-1 );
  26. Echo $ result;
  27. ?>

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:

  1. Array (
  2. [0] => 0
  3. [1] => 1
  4. [2] => 2
  5. [3] => 3
  6. [4] => 4
  7. [5] => 5
  8. [6] => 6
  9. [7] => 7
  10. [8] => 8
  11. [9] => 9
  12. [10] =>
  13. [11] => B
  14. [12] => c
  15. [13] => d
  16. [14] => e
  17. [15] => f
  18. [16] => g
  19. [17] => h
  20. [18] => I
  21. [19] => j
  22. [20] => k
  23. [21] => l
  24. [22] => m
  25. [23] => n
  26. [24] => o
  27. [25] => p
  28. [26] => q
  29. [27] => r
  30. [28] => s
  31. [29] => t
  32. [30] => u
  33. [31] => v
  34. [32] => w
  35. [33] => x
  36. [34] => y
  37. [35] => z
  38. )

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:

  1. // Generate a character in 0123456789abcdefghijklmnopqrstuvwxyz
  2. Function getOptions ()
  3. {
  4. $ Options = array ();
  5. $ Result = array ();
  6. For ($ I = 48; $ I <= 57; $ I ++)
  7. {
  8. Array_push ($ options, chr ($ I ));
  9. }
  10. For ($ I = 65; $ I <= 90; $ I ++)
  11. {
  12. $ J = 32;
  13. $ Small = $ I + $ j;
  14. Array_push ($ options, chr ($ small ));
  15. }
  16. Return $ options;
  17. }
  18. /*
  19. $ E = getOptions ();
  20. For ($ j = 0; $ j <150; $ j ++)
  21. {
  22. Echo $ e [$ j];
  23. }
  24. */
  25. $ Len = 10;
  26. // Randomly generate an array index to achieve a random number
  27. For ($ j = 0; $ j <100; $ j ++)
  28. {
  29. $ Result = "";
  30. $ Options = getOptions ();
  31. $ LastIndex = 35;
  32. While (strlen ($ result) <$ len)
  33. {
  34. // Random index from 0 to 35
  35. $ Index = rand (0, $ lastIndex );
  36. // Assign the random number to the variable $ chr
  37. $ Chr = $ options [$ index];
  38. // Random number as part of $ result
  39. $ Result. = $ chr;
  40. $ LastIndex = $ lastIndex-1;
  41. // The last index will not be used in the next random lottery.
  42. $ Options [$ index] = $ options [$ lastIndex];
  43. }
  44. Echo $ result. "n ";
  45. }
  46. ?>

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.