PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/standard/rand.c

http://github.com/php/php-src
C | 68 lines | 28 code | 8 blank | 32 comment | 3 complexity | c7c902d25edec46762ad50edf5dfb19a MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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. | http://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. */
  29. PHPAPI void php_srand(zend_long seed)
  30. {
  31. php_mt_srand(seed);
  32. }
  33. /* }}} */
  34. /* {{{ php_rand
  35. */
  36. PHPAPI zend_long php_rand(void)
  37. {
  38. return php_mt_rand();
  39. }
  40. /* }}} */
  41. /* {{{ proto int mt_rand([int min, int max])
  42. Returns a random number from Mersenne Twister */
  43. PHP_FUNCTION(rand)
  44. {
  45. zend_long min;
  46. zend_long max;
  47. int argc = ZEND_NUM_ARGS();
  48. if (argc == 0) {
  49. RETURN_LONG(php_mt_rand() >> 1);
  50. }
  51. ZEND_PARSE_PARAMETERS_START(2, 2)
  52. Z_PARAM_LONG(min)
  53. Z_PARAM_LONG(max)
  54. ZEND_PARSE_PARAMETERS_END();
  55. if (max < min) {
  56. RETURN_LONG(php_mt_rand_common(max, min));
  57. }
  58. RETURN_LONG(php_mt_rand_common(min, max));
  59. }
  60. /* }}} */