rand.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Rasmus Lerdorf <rasmus@php.net> |
  14. | Zeev Suraski <zeev@php.net> |
  15. | Pedro Melo <melo@ip.pt> |
  16. | Sterling Hughes <sterling@php.net> |
  17. | |
  18. | Based on code from: Richard J. Wagner <rjwagner@writeme.com> |
  19. | Makoto Matsumoto <matumoto@math.keio.ac.jp> |
  20. | Takuji Nishimura |
  21. | Shawn Cokus <Cokus@math.washington.edu> |
  22. +----------------------------------------------------------------------+
  23. */
  24. #include "php.h"
  25. #include "php_rand.h"
  26. #include "php_mt_rand.h"
  27. /* {{{ php_srand */
  28. PHPAPI void php_srand(zend_long seed)
  29. {
  30. php_mt_srand(seed);
  31. }
  32. /* }}} */
  33. /* {{{ php_rand */
  34. PHPAPI zend_long php_rand(void)
  35. {
  36. return php_mt_rand();
  37. }
  38. /* }}} */
  39. /* {{{ Returns a random number from Mersenne Twister */
  40. PHP_FUNCTION(rand)
  41. {
  42. zend_long min;
  43. zend_long max;
  44. int argc = ZEND_NUM_ARGS();
  45. if (argc == 0) {
  46. RETURN_LONG(php_mt_rand() >> 1);
  47. }
  48. ZEND_PARSE_PARAMETERS_START(2, 2)
  49. Z_PARAM_LONG(min)
  50. Z_PARAM_LONG(max)
  51. ZEND_PARSE_PARAMETERS_END();
  52. if (max < min) {
  53. RETURN_LONG(php_mt_rand_common(max, min));
  54. }
  55. RETURN_LONG(php_mt_rand_common(min, max));
  56. }
  57. /* }}} */