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