php_rand.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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: Shawn Cokus <Cokus@math.washington.edu> |
  21. +----------------------------------------------------------------------+
  22. */
  23. #ifndef PHP_RAND_H
  24. #define PHP_RAND_H
  25. #include "php_lcg.h"
  26. #include "php_mt_rand.h"
  27. /* System Rand functions */
  28. #ifndef RAND_MAX
  29. #define RAND_MAX PHP_MT_RAND_MAX
  30. #endif
  31. #define PHP_RAND_MAX PHP_MT_RAND_MAX
  32. /*
  33. * A bit of tricky math here. We want to avoid using a modulus because
  34. * that simply tosses the high-order bits and might skew the distribution
  35. * of random values over the range. Instead we map the range directly.
  36. *
  37. * We need to map the range from 0...M evenly to the range a...b
  38. * Let n = the random number and n' = the mapped random number
  39. *
  40. * Then we have: n' = a + n(b-a)/M
  41. *
  42. * We have a problem here in that only n==M will get mapped to b which
  43. # means the chances of getting b is much much less than getting any of
  44. # the other values in the range. We can fix this by increasing our range
  45. # artificially and using:
  46. #
  47. # n' = a + n(b-a+1)/M
  48. *
  49. # Now we only have a problem if n==M which would cause us to produce a
  50. # number of b+1 which would be bad. So we bump M up by one to make sure
  51. # this will never happen, and the final algorithm looks like this:
  52. #
  53. # n' = a + n(b-a+1)/(M+1)
  54. *
  55. * -RL
  56. */
  57. #define RAND_RANGE_BADSCALING(__n, __min, __max, __tmax) \
  58. (__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
  59. #ifdef PHP_WIN32
  60. #define GENERATE_SEED() (((zend_long) (time(0) * GetCurrentProcessId())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
  61. #else
  62. #define GENERATE_SEED() (((zend_long) (time(0) * getpid())) ^ ((zend_long) (1000000.0 * php_combined_lcg())))
  63. #endif
  64. PHPAPI void php_srand(zend_long seed);
  65. PHPAPI zend_long php_rand(void);
  66. #endif /* PHP_RAND_H */