|
21 | 21 | */
|
22 | 22 | /* $Id$ */
|
23 | 23 |
|
| 24 | +/* Layout implementation random functions |
| 25 | + * |
| 26 | + * The PHPAPI contains these functions: |
| 27 | + * - long php_rand() |
| 28 | + * - long php_rand_range(long min, long max) |
| 29 | + * - void php_srand() |
| 30 | + * - long php_getrandmax() |
| 31 | + * |
| 32 | + * Note that it is not possible to choose the algoritm. This is done to |
| 33 | + * give the user the possibility to control all randomness by means of |
| 34 | + * srand()/php.ini in a portable and consistent way. |
| 35 | + * |
| 36 | + * rand.c: (the only rand*.c file with PHP_API and PHP_FUNCTION functions) |
| 37 | + * |
| 38 | + * - PHP_FUNCTION([mt_]srand) |
| 39 | + * +-> void php_srand(void) |
| 40 | + * +-> void php_srand2(long seed, int alg) |
| 41 | + * +-> (rand_sys.c) long php_rand_sys() |
| 42 | + * +-> (rand_mt.c ) long php_rand_mt() |
| 43 | + * |
| 44 | + * - PHP_FUNCTION([mt_]rand) |
| 45 | + * +-> long php_rand() |
| 46 | + * +-> (rand_sys.c) long php_rand_sys() |
| 47 | + * +-> (rand_mt.c ) long php_rand_mt() |
| 48 | + * +-> long php_rand_range(long min, long max) |
| 49 | + * +-> calls php_rand() |
| 50 | + * |
| 51 | + * - PHP_FUNCTION([mt_]getrandmax) |
| 52 | + * +-> PHPAPI long php_randmax(void) |
| 53 | + * +-> (rand_sys.c) long php_randmax_sys() |
| 54 | + * +-> (rand_mt.c ) long php_randmax_mt() |
| 55 | + * |
| 56 | + * --Jeroen |
| 57 | + */ |
| 58 | + |
24 | 59 | #ifndef PHP_RAND_H
|
25 | 60 | #define PHP_RAND_H
|
26 | 61 |
|
|
31 | 66 | #endif
|
32 | 67 |
|
33 | 68 | #if HAVE_LRAND48
|
34 |
| -#define PHP_RAND_MAX 2147483647 |
| 69 | +#define php_randmax_sys() 2147483647 |
35 | 70 | #else
|
36 |
| -#define PHP_RAND_MAX RAND_MAX |
| 71 | +#define php_randmax_sys() RAND_MAX |
37 | 72 | #endif
|
38 | 73 |
|
| 74 | +/* |
| 75 | + * Melo: it could be 2^^32 but we only use 2^^31 to maintain |
| 76 | + * compatibility with the previous php_rand |
| 77 | + */ |
| 78 | +#define php_randmax_mt() ((long)(0x7FFFFFFF)) /* 2^^31 - 1 */ |
| 79 | + |
| 80 | +PHP_FUNCTION(srand); |
| 81 | +PHP_FUNCTION(rand); |
| 82 | +PHP_FUNCTION(getrandmax); |
| 83 | +PHP_FUNCTION(mt_srand); |
| 84 | +PHP_FUNCTION(mt_rand); |
| 85 | +PHP_FUNCTION(mt_getrandmax); |
| 86 | + |
| 87 | +PHPAPI long php_rand(void); |
| 88 | +PHPAPI long php_rand_range(long min, long max); |
| 89 | +PHPAPI long php_randmax(void); |
| 90 | +long php_rand_mt(void); |
| 91 | +void php_srand_mt(long seed TSRMLS_DC); |
| 92 | + |
39 | 93 | /* Define rand Function wrapper */
|
40 | 94 | #ifdef HAVE_RANDOM
|
41 |
| -#define php_rand() random() |
| 95 | +#define php_rand_sys() random() |
42 | 96 | #else
|
43 | 97 | #ifdef HAVE_LRAND48
|
44 |
| -#define php_rand() lrand48() |
| 98 | +#define php_rand_sys() lrand48() |
45 | 99 | #else
|
46 |
| -#define php_rand() rand() |
| 100 | +#define php_rand_sys() rand() |
47 | 101 | #endif
|
48 | 102 | #endif
|
49 | 103 |
|
50 | 104 | /* Define srand Function wrapper */
|
51 | 105 | #ifdef HAVE_SRANDOM
|
52 |
| -#define php_srand(seed) srandom((unsigned int)seed) |
| 106 | +#define php_srand_sys(seed) srandom((unsigned int)seed) |
53 | 107 | #else
|
54 | 108 | #ifdef HAVE_SRAND48
|
55 |
| -#define php_srand(seed) srand48((long)seed) |
| 109 | +#define php_srand_sys(seed) srand48((long)seed) |
56 | 110 | #else
|
57 |
| -#define php_srand(seed) srand((unsigned int)seed) |
| 111 | +#define php_srand_sys(seed) srand((unsigned int)seed) |
58 | 112 | #endif
|
59 | 113 | #endif
|
60 | 114 |
|
| 115 | +/* Define random generator constants */ |
| 116 | +#define RAND_SYS 1 |
| 117 | +#define RAND_MT 2 |
| 118 | + |
| 119 | +/* BC */ |
| 120 | +#define PHP_RAND_MAX php_randmax() |
| 121 | + |
61 | 122 | #endif /* PHP_RAND_H */
|
| 123 | + |
| 124 | +/* |
| 125 | + * Local variables: |
| 126 | + * tab-width: 4 |
| 127 | + * c-basic-offset: 4 |
| 128 | + * End: |
| 129 | + * vim600: fdm=marker |
| 130 | + * vim: sw=4 ts=4 tw=78 |
| 131 | + */ |
0 commit comments