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