php_rand.h 3.0 KB

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