Home > Backend Development > PHP Tutorial > php lottery applet

php lottery applet

WBOY
Release: 2016-07-25 09:01:32
Original
1300 people have browsed it
This lottery applet can also be used in actual testing environments. For example, when querying test data under the in condition, you need to query 5 random IDs, and then use ab to perform stress testing.
  1. /**
  2. * "Lottery" function
  3. *
  4. * @param integer $first starting number
  5. * @param integer $last ending number
  6. * @param integer $total number of winners
  7. *
  8. * @return string
  9. *
  10. */
  11. function isWinner($first, $last, $total)
  12. {
  13. $winner = array();
  14. for ($i=0;;$ i++)
  15. {
  16. $number = mt_rand($first, $last);
  17. if (!in_array($number, $winner))
  18. $winner[] = $number; // If the number is not in the array, replace it Add to array
  19. if (count($winner) == $total) break;
  20. }
  21. return implode(' ', $winner);
  22. }
  23. // for test
  24. echo isWinner(1, 100, 5);
  25. ?>
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template