15
15
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
16
16
| Zeev Suraski <zeev@zend.com> |
17
17
| Pedro Melo <melo@ip.pt> |
18
+ | Jeroen van Wolffelaar <jeroen@php.net> |
18
19
| |
19
20
| Based on code from: Shawn Cokus <Cokus@math.washington.edu> |
20
21
+----------------------------------------------------------------------+
56
57
* --Jeroen
57
58
*/
58
59
60
+ /* TODO:
61
+ * - make constants available to PHP-user
62
+ * - MINFO section about which random number generators are available
63
+ * - Nuke randmax by enhancing PHP_RAND_RANGE to work well in the case of a
64
+ * greater request than the real (internal) randmax is
65
+ * - Implement LCG
66
+ * - Implement a real-random source? (via internet, and/or /dev/urandom?)
67
+ * - Can lrand48 be thread-safe?
68
+ * - Is random() useful sometimes?
69
+ * - Which system algorithms are available, maybe name them after real
70
+ * algorithm by compile-time detection?
71
+ * - Get this to compile :-)
72
+ */
59
73
#ifndef PHP_RAND_H
60
74
#define PHP_RAND_H
61
75
62
76
#include <stdlib.h>
63
77
64
- #ifndef RAND_MAX
65
- #define RAND_MAX (1<<15)
66
- #endif
78
+ /* FIXME: that '_php_randgen_entry' needed, or not? */
79
+ typedef struct _php_randgen_entry {
80
+ void (* srand )(long seed );
81
+ long (* rand )(void );
82
+ long randmax ;
83
+ char * ini_str ;
84
+ } php_randgen_entry ;
67
85
68
- #if HAVE_LRAND48
69
- #define php_randmax_sys () 2147483647
70
- #else
71
- #define php_randmax_sys () RAND_MAX
72
- #endif
86
+ php_randgen_entry * php_randgen_entries ;
73
87
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 */
88
+ #define PHP_SRAND (which ,seed ) (php_randgen_entry[which]->srand(seed))
89
+ #define PHP_RAND (which ) (php_randgen_entry[which]->rand())
90
+ #define PHP_RANDMAX (which ) (php_randgen_entry[which].randmax)
91
+ #define PHP_RAND_INISTR (which ) (php_randgen_entry[which].ini_str)
92
+
93
+ /* Define random generator constants */
94
+ #define PHP_RAND_SYS 0
95
+ #define PHP_RAND_LRAND48 1
96
+ #define PHP_RAND_MT 2
97
+ #define PHP_RAND_LCG 3
98
+
99
+ #define PHP_RAND_DEFAULT PHP_RAND_MT
100
+
101
+ /* how many there are */
102
+ #define PHP_RAND_NUMRANDS 4
79
103
104
+ /* Proto's */
80
105
PHP_FUNCTION (srand );
81
106
PHP_FUNCTION (rand );
82
107
PHP_FUNCTION (getrandmax );
@@ -87,45 +112,6 @@ PHP_FUNCTION(mt_getrandmax);
87
112
PHPAPI long php_rand (void );
88
113
PHPAPI long php_rand_range (long min , long max );
89
114
PHPAPI long php_randmax (void );
90
- long php_rand_mt (void );
91
- void php_srand_mt (long seed TSRMLS_DC );
92
-
93
- /* Define rand Function wrapper */
94
- #ifdef HAVE_RANDOM
95
- #define php_rand_sys () random()
96
- #else
97
- #ifdef HAVE_LRAND48
98
- #define php_rand_sys () lrand48()
99
- #else
100
- #define php_rand_sys () rand()
101
- #endif
102
- #endif
103
-
104
- /* Define srand Function wrapper */
105
- #ifdef HAVE_SRANDOM
106
- #define php_srand_sys (seed ) srandom((unsigned int)seed)
107
- #else
108
- #ifdef HAVE_SRAND48
109
- #define php_srand_sys (seed ) srand48((long)seed)
110
- #else
111
- #define php_srand_sys (seed ) srand((unsigned int)seed)
112
- #endif
113
- #endif
114
-
115
- /* Define random generator constants */
116
- #define RAND_SYS 1
117
- #define RAND_MT 2
118
- #define RAND_LCG 3
119
- #define RAND_SYS_STR "system"
120
- #define RAND_MT_STR "mt"
121
- #define RAND_LCG_STR "lcg"
122
-
123
- #define RAND_DEFAULT RAND_MT
124
- #define RAND_DEFAULT_STR RAND_MT_STR
125
-
126
-
127
- /* BC */
128
- #define PHP_RAND_MAX php_randmax()
129
115
130
116
#endif /* PHP_RAND_H */
131
117
0 commit comments