summaryrefslogtreecommitdiff
path: root/ext/standard/rand.c
blob: 5e1c39fbdc4385656154fc5d8ccf576ec0c50655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
   |          Zeev Suraski <zeev@zend.com>                                |
   |          Pedro Melo <melo@ip.pt>                                     |
   +----------------------------------------------------------------------+
 */
/* $Id$ */

#include <stdlib.h>

#include "php.h"
#include "php_math.h"
#include "php_rand.h"

#include "zend_execute.h"

#include "basic_functions.h"

/* See php_rand.h for information about layout */

/* srand */

/* {{{ PHPAPI void php_srand(void) */
PHPAPI void php_srand(void)
{
	/* php_srand_2( 1e6d * microtime() , protocol-specified-in-php.ini ) */
	php_error(E_ERROR,"Not yet implemented");
}
/* }}} */

/* {{{ void php_srand2(long seed, int alg) */
static inline void php_srand2(long seed, int alg)
{
	switch (alg) {
		case RAND_SYS:
			php_srand_sys(seed);
			return;
		case RAND_MT:
			php_srand_mt(seed TSRMLS_CC);
			return;
		default:
			php_error(E_ERROR,"Bug, please report (php_srand2-%d)",alg);
			return;
	}
}
/* }}} */

/* {{{ [mt_]srand common */
#define pim_srand_common(name,type) 				\
PHP_FUNCTION(name)							\
{											\
	zval **arg;								\
											\
	if (ZEND_NUM_ARGS() != 1) {				\
		WRONG_PARAM_COUNT;					\
	}										\
	zend_get_parameters_ex(1, &arg);		\
	convert_to_long_ex(arg);				\
											\
	php_srand2((*arg)->value.lval, type); 	\
}
/* }}} */

/* {{{ proto void srand(int seed)
   Seeds random number generator */
pim_srand_common(srand,RAND_SYS)
/* }}} */

/* {{{ proto void mt_srand(int seed)
   Seeds random number generator */
pim_srand_common(mt_srand,RAND_MT)
/* }}} */

/* rand */

/* {{{ PHPAPI long php_rand(void) */
PHPAPI long php_rand(void)
{
	/* algorithm = BG(current_alg) */
	return php_rand_sys();
}
/* }}} */

/* {{{ macro: php_map_a_range */
#define php_map_a_range(number,min,max,MAX) {  \
    /*
     * A bit of tricky math here.  We want to avoid using a modulus because
     * that simply tosses the high-order bits and might skew the distribution
     * of random values over the range.  Instead we map the range directly.
     *
     * We need to map the range from 0...M evenly to the range a...b
	 * Expressed in real numbers, this becomes:
	 *
	 * 				[0,M+1[ mapped to [a,b+1[
	 * 
     * Let n = the random number and n' = the mapped random number
	 * So the formula needs to be:
	 *
     *               n' = a + n((b+1)-a)/(m+1) 
     *
	 *  This isn't perfect, because n only takes integer values. So when a..b
	 *  spans a significant portion of 0..M, some numbers have nearly twice as
	 *  much chance. But since twice a very small chance is still a very small
	 *  chance, it's ignored.
	 *
	 *  --Rasmus and Jeroen
     */                            \
	if ((max) < (min)) {		\
		php_error(E_WARNING, "%s():  Invalid range:  %ld..%ld (minimum can't be larger than maximum)", \
			get_active_function_name(TSRMLS_C), (min), (max)); \
	} else if ( (max) - (min) > (MAX) ) { \
		/* TODO: this can done better, get two numbers and combine... */ \
		php_error(E_WARNING, "%s():  Invalid range:  %ld..%ld (can't give that much randomness)",  \
			get_active_function_name(TSRMLS_C), (min), (max)); \
	} \
	(number) = (min) + (int) ((double)((max)-(min)+1) * (number)/(MAX+1.0)); \
}
/* }}} */

/* {{{ long php_rand_range2(long min, long max, int alg) 
 *  Temporary hack function */
static inline long php_rand_range2(long min, long max, int alg)
{
	long number;
	
	switch (alg) {
		case RAND_SYS:
			number = php_rand_sys();
			php_map_a_range(number,min,max,php_randmax_sys());
			return number;
		case RAND_MT:
			number = php_rand_mt();
			php_map_a_range(number,min,max,php_randmax_mt());
			return number;
		default:
			php_error(E_ERROR,"Bug, please report (php_rand_range2-%d)",alg);
			return;
	}
}
/* }}} */

/* {{{ PHPAPI long php_rand_range(long min, long max) */
PHPAPI long php_rand_range(long min, long max)
{
	/* will be php.ini & srand aware */
	return php_rand_range2(min,max,RAND_SYS);
}
/* }}} */

/* {{{ [mt_]rand common */
#define PHP_FUNCTION_RAND(name,type,lowertype)   					\
PHP_FUNCTION(name)													\
{																	\
	zval **min, **max;												\
																	\
	switch (ZEND_NUM_ARGS()) {										\
		case 0:														\
			RETURN_LONG(php_rand_##lowertype());					\
		case 2:														\
			if (zend_get_parameters_ex(2, &min, &max)==FAILURE) {	\
				RETURN_FALSE;										\
			}														\
			convert_to_long_ex(min);								\
			convert_to_long_ex(max);								\
			RETURN_LONG(php_rand_range2(Z_LVAL_PP(min), 			\
						Z_LVAL_PP(max), type));				\
		default:													\
			WRONG_PARAM_COUNT;										\
			break;													\
	}																\
}
/* }}} */

/* {{{ proto int rand([int min, int max]) 
   Returns a random number */
PHP_FUNCTION_RAND(rand,RAND_SYS,sys)
/* }}} */

/* {{{ proto int mt_rand([int min, int max]) 
   Returns a random number by means of Mersenne Twister */
PHP_FUNCTION_RAND(mt_rand,RAND_MT,mt)
/* }}} */

/* getrandmax */

/* {{{ PHPAPI long php_randmax(void)
   Returns the maximum value a random number can have */
PHPAPI long php_randmax(void)
{
	/* Get current algorithm */
	return php_randmax_sys();
}
/* }}} */

/* {{{ proto int getrandmax(void)
   Returns the maximum value a random number can have */
PHP_FUNCTION(getrandmax)
{
	if (ZEND_NUM_ARGS() != 0) {
		WRONG_PARAM_COUNT;
	}

	RETURN_LONG( php_randmax_sys() );
}
/* }}} */

/* {{{ proto int mt_getrandmax(void)
   Returns the maximum value a random number can have */
PHP_FUNCTION(mt_getrandmax)
{
	if (ZEND_NUM_ARGS() != 0) {
		WRONG_PARAM_COUNT;
	}

	RETURN_LONG( php_randmax_mt() );
}
/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: sw=4 ts=4 tw=78 fdm=marker
 * vim<600: sw=4 ts=4 tw=78
 */