rand.c 2.5 KB

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