Home > Backend Development > PHP Tutorial > Simple php lottery code

Simple php lottery code

WBOY
Release: 2016-07-25 08:45:50
Original
1196 people have browsed it
  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

php


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