Skip to content

Commit d101bd3

Browse files
author
Jeroen van Wolffelaar
committed
Implement the proposal v2
Added PHPAPI php_drand, which returns a double in the range [0,1) RAND_REDESIGN completed, prohibited some minor points. Ready to be re-entered in MAIN branch
1 parent 183cf93 commit d101bd3

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

ext/standard/php_rand.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ PHP_FUNCTION(mt_srand);
113113
PHP_FUNCTION(mt_rand);
114114
PHP_FUNCTION(mt_getrandmax);
115115

116-
PHPAPI long php_rand(void);
117-
PHPAPI long php_rand_range(long min, long max);
118-
PHPAPI long php_randmax(void);
116+
PHPAPI long php_rand(void);
117+
PHPAPI long php_rand_range(long min, long max);
118+
PHPAPI double php_drand(void);
119+
PHPAPI long php_randmax(void);
119120

120121
#endif /* PHP_RAND_H */
121122

ext/standard/rand.c

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ php_randgen_entry *php_randgen_entries[PHP_RAND_NUMRANDS];
4141
#define PHP_RANDMAX(which) (php_randgen_entries[which]->randmax)
4242
#define PHP_RAND_INISTR(which) (php_randgen_entries[which]->ini_str)
4343

44+
#define CURR_GEN BG(rand_generator_current)
45+
4446
PHP_MINIT_FUNCTION(rand)
4547
{
4648
PHP_MINIT(rand_sys)(INIT_FUNC_ARGS_PASSTHRU);
@@ -110,7 +112,7 @@ PHP_INI_END()
110112
/* {{{ PHPAPI void php_srand(void) */
111113
PHPAPI void php_srand(void)
112114
{
113-
BG(rand_generator_current) = BG(rand_generator);
115+
CURR_GEN = BG(rand_generator);
114116
PHP_SRAND(BG(rand_generator), SRAND_A_RANDOM_SEED);
115117
}
116118
/* }}} */
@@ -119,25 +121,49 @@ PHPAPI void php_srand(void)
119121
#define pim_srand_common(name,type) \
120122
PHP_FUNCTION(name) \
121123
{ \
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; \
129126
\
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+
} \
132158
}
133159
/* }}} */
134160

135-
/* {{{ proto void srand(int seed)
161+
/* {{{ proto bool srand(int seed)
136162
Seeds random number generator */
137163
pim_srand_common(srand,PHP_RAND_SYS)
138164
/* }}} */
139165

140-
/* {{{ proto void mt_srand(int seed)
166+
/* {{{ proto bool mt_srand(int seed)
141167
Seeds random number generator */
142168
pim_srand_common(mt_srand,PHP_RAND_MT)
143169
/* }}} */
@@ -147,7 +173,16 @@ pim_srand_common(mt_srand,PHP_RAND_MT)
147173
/* {{{ PHPAPI long php_rand(void) */
148174
PHPAPI long php_rand(void)
149175
{
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);
151186
}
152187
/* }}} */
153188

@@ -192,7 +227,8 @@ PHPAPI long php_rand(void)
192227
PHPAPI long php_rand_range(long min, long max)
193228
{
194229
register long result;
195-
PHP_RAND_RANGE(BG(rand_generator_current), min, max, result);
230+
231+
PHP_RAND_RANGE(CURR_GEN, min, max, result);
196232
return result;
197233
}
198234
/* }}} */
@@ -239,7 +275,7 @@ PHP_FUNCTION_RAND(mt_rand,PHP_RAND_MT)
239275
Returns the maximum value a random number can have */
240276
PHPAPI long php_randmax(void)
241277
{
242-
return PHP_RANDMAX(BG(rand_generator_current));
278+
return PHP_RANDMAX(CURR_GEN);
243279
}
244280
/* }}} */
245281

@@ -251,7 +287,7 @@ PHP_FUNCTION(getrandmax)
251287
WRONG_PARAM_COUNT;
252288
}
253289

254-
RETURN_LONG( PHP_RANDMAX(PHP_RAND_SYS));
290+
RETURN_LONG( php_randmax());
255291
}
256292
/* }}} */
257293

@@ -263,7 +299,7 @@ PHP_FUNCTION(mt_getrandmax)
263299
WRONG_PARAM_COUNT;
264300
}
265301

266-
RETURN_LONG( PHP_RANDMAX(PHP_RAND_MT));
302+
RETURN_LONG( php_randmax() );
267303
}
268304
/* }}} */
269305

0 commit comments

Comments
 (0)