@@ -41,6 +41,8 @@ php_randgen_entry *php_randgen_entries[PHP_RAND_NUMRANDS];
41
41
#define PHP_RANDMAX (which ) (php_randgen_entries[which]->randmax)
42
42
#define PHP_RAND_INISTR (which ) (php_randgen_entries[which]->ini_str)
43
43
44
+ #define CURR_GEN BG(rand_generator_current)
45
+
44
46
PHP_MINIT_FUNCTION (rand )
45
47
{
46
48
PHP_MINIT (rand_sys )(INIT_FUNC_ARGS_PASSTHRU );
@@ -110,7 +112,7 @@ PHP_INI_END()
110
112
/* {{{ PHPAPI void php_srand(void) */
111
113
PHPAPI void php_srand (void )
112
114
{
113
- BG ( rand_generator_current ) = BG (rand_generator );
115
+ CURR_GEN = BG (rand_generator );
114
116
PHP_SRAND (BG (rand_generator ), SRAND_A_RANDOM_SEED );
115
117
}
116
118
/* }}} */
@@ -119,25 +121,49 @@ PHPAPI void php_srand(void)
119
121
#define pim_srand_common (name ,type ) \
120
122
PHP_FUNCTION(name) \
121
123
{ \
122
- zval **arg; \
123
- \
124
- if (ZEND_NUM_ARGS() != 1) { \
125
- WRONG_PARAM_COUNT; \
126
- } \
127
- zend_get_parameters_ex(1, &arg); \
128
- convert_to_long_ex(arg); \
124
+ zval **seed; \
125
+ zval **alg; \
129
126
\
130
- BG(rand_generator_current) = type; \
131
- PHP_SRAND(type, Z_LVAL_PP(arg)); \
127
+ switch (ZEND_NUM_ARGS()) { \
128
+ case 0: \
129
+ CURR_GEN = BG(rand_generator); \
130
+ PHP_SRAND(BG(rand_generator), SRAND_A_RANDOM_SEED); \
131
+ RETURN_TRUE; \
132
+ case 1: \
133
+ zend_get_parameters_ex(1, &seed); \
134
+ convert_to_long_ex(seed); \
135
+ CURR_GEN = type; \
136
+ PHP_SRAND(type, Z_LVAL_PP(seed)); \
137
+ RETURN_TRUE; \
138
+ case 2: \
139
+ /* algorithm, seed is most logic, though it is the other way
140
+ * around than current way... */ \
141
+ zend_get_parameters_ex (2 , & alg , & seed ); \
142
+ convert_to_long_ex (seed ); \
143
+ convert_to_long_ex (alg ); \
144
+ if (0 > Z_LVAL_PP (alg ) || Z_LVAL_PP (alg ) >= PHP_RAND_NUMRANDS ) { \
145
+ php_error (E_WARNING , "%s(): There is no algorithm %d." , get_active_function_name (TSRMLS_C ), Z_LVAL_PP (alg )); \
146
+ RETURN_FALSE ; \
147
+ } \
148
+ if (!PHP_HAS_SRAND (Z_LVAL_PP (alg ))) { \
149
+ php_error (E_WARNING , "%s(): Algorithm %d does not support reproducable results." , get_active_function_name (TSRMLS_C ), Z_LVAL_PP (alg )); \
150
+ RETURN_FALSE ; \
151
+ } \
152
+ CURR_GEN = Z_LVAL_PP (alg ); \
153
+ PHP_SRAND (Z_LVAL_PP (alg ), Z_LVAL_PP (seed )); \
154
+ RETURN_TRUE ; \
155
+ default : \
156
+ WRONG_PARAM_COUNT ; \
157
+ } \
132
158
}
133
159
/* }}} */
134
160
135
- /* {{{ proto void srand(int seed)
161
+ /* {{{ proto bool srand(int seed)
136
162
Seeds random number generator */
137
163
pim_srand_common (srand ,PHP_RAND_SYS )
138
164
/* }}} */
139
165
140
- /* {{{ proto void mt_srand(int seed)
166
+ /* {{{ proto bool mt_srand(int seed)
141
167
Seeds random number generator */
142
168
pim_srand_common (mt_srand ,PHP_RAND_MT )
143
169
/* }}} */
@@ -147,7 +173,16 @@ pim_srand_common(mt_srand,PHP_RAND_MT)
147
173
/* {{{ PHPAPI long php_rand(void) */
148
174
PHPAPI long php_rand (void )
149
175
{
150
- return PHP_RAND (BG (rand_generator_current ));
176
+ return PHP_RAND (CURR_GEN );
177
+ }
178
+ /* }}} */
179
+
180
+ /* {{{ PHPAPI double php_drand(void)
181
+ * returns a double in the range [0,1) */
182
+ PHPAPI double php_drand (void )
183
+ {
184
+ return (double )php_rand () /
185
+ (double )(PHP_RANDMAX (CURR_GEN )+ 1.0 );
151
186
}
152
187
/* }}} */
153
188
@@ -192,7 +227,8 @@ PHPAPI long php_rand(void)
192
227
PHPAPI long php_rand_range (long min , long max )
193
228
{
194
229
register long result ;
195
- PHP_RAND_RANGE (BG (rand_generator_current ), min , max , result );
230
+
231
+ PHP_RAND_RANGE (CURR_GEN , min , max , result );
196
232
return result ;
197
233
}
198
234
/* }}} */
@@ -239,7 +275,7 @@ PHP_FUNCTION_RAND(mt_rand,PHP_RAND_MT)
239
275
Returns the maximum value a random number can have */
240
276
PHPAPI long php_randmax (void )
241
277
{
242
- return PHP_RANDMAX (BG ( rand_generator_current ) );
278
+ return PHP_RANDMAX (CURR_GEN );
243
279
}
244
280
/* }}} */
245
281
@@ -251,7 +287,7 @@ PHP_FUNCTION(getrandmax)
251
287
WRONG_PARAM_COUNT ;
252
288
}
253
289
254
- RETURN_LONG ( PHP_RANDMAX ( PHP_RAND_SYS ));
290
+ RETURN_LONG ( php_randmax ( ));
255
291
}
256
292
/* }}} */
257
293
@@ -263,7 +299,7 @@ PHP_FUNCTION(mt_getrandmax)
263
299
WRONG_PARAM_COUNT ;
264
300
}
265
301
266
- RETURN_LONG ( PHP_RANDMAX ( PHP_RAND_MT ) );
302
+ RETURN_LONG ( php_randmax () );
267
303
}
268
304
/* }}} */
269
305
0 commit comments