Skip to content

Commit 8d6a9b0

Browse files
author
Jeroen van Wolffelaar
committed
Experimental beginning of a change in the way rand-functions work.
This is only a _move around_ of code into functions.
1 parent 6e8ad1f commit 8d6a9b0

File tree

5 files changed

+441
-283
lines changed

5 files changed

+441
-283
lines changed

ext/standard/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LTLIBRARY_SOURCES=\
55
dir.c dl.c dns.c exec.c file.c filestat.c flock_compat.c \
66
formatted_print.c fsock.c head.c html.c image.c info.c iptc.c lcg.c \
77
link.c mail.c math.c md5.c metaphone.c microtime.c pack.c pageinfo.c \
8-
parsedate.c quot_print.c rand.c reg.c soundex.c string.c scanf.c \
8+
parsedate.c quot_print.c rand.c rand_mt.c reg.c soundex.c string.c scanf.c \
99
syslog.c type.c uniqid.c url.c url_scanner.c var.c assert.c \
1010
strnatcmp.c levenshtein.c incomplete_class.c url_scanner_ex.c \
1111
ftp_fopen_wrapper.c http_fopen_wrapper.c php_fopen_wrapper.c credits.c

ext/standard/php_math.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ PHP_FUNCTION(log);
3434
PHP_FUNCTION(log10);
3535
PHP_FUNCTION(pow);
3636
PHP_FUNCTION(sqrt);
37-
PHP_FUNCTION(srand);
38-
PHP_FUNCTION(rand);
39-
PHP_FUNCTION(getrandmax);
40-
PHP_FUNCTION(mt_srand);
41-
PHP_FUNCTION(mt_rand);
42-
PHP_FUNCTION(mt_getrandmax);
4337
PHP_FUNCTION(abs);
4438
PHP_FUNCTION(ceil);
4539
PHP_FUNCTION(floor);

ext/standard/php_rand.h

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@
2121
*/
2222
/* $Id$ */
2323

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+
2459
#ifndef PHP_RAND_H
2560
#define PHP_RAND_H
2661

@@ -31,31 +66,66 @@
3166
#endif
3267

3368
#if HAVE_LRAND48
34-
#define PHP_RAND_MAX 2147483647
69+
#define php_randmax_sys() 2147483647
3570
#else
36-
#define PHP_RAND_MAX RAND_MAX
71+
#define php_randmax_sys() RAND_MAX
3772
#endif
3873

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+
3993
/* Define rand Function wrapper */
4094
#ifdef HAVE_RANDOM
41-
#define php_rand() random()
95+
#define php_rand_sys() random()
4296
#else
4397
#ifdef HAVE_LRAND48
44-
#define php_rand() lrand48()
98+
#define php_rand_sys() lrand48()
4599
#else
46-
#define php_rand() rand()
100+
#define php_rand_sys() rand()
47101
#endif
48102
#endif
49103

50104
/* Define srand Function wrapper */
51105
#ifdef HAVE_SRANDOM
52-
#define php_srand(seed) srandom((unsigned int)seed)
106+
#define php_srand_sys(seed) srandom((unsigned int)seed)
53107
#else
54108
#ifdef HAVE_SRAND48
55-
#define php_srand(seed) srand48((long)seed)
109+
#define php_srand_sys(seed) srand48((long)seed)
56110
#else
57-
#define php_srand(seed) srand((unsigned int)seed)
111+
#define php_srand_sys(seed) srand((unsigned int)seed)
58112
#endif
59113
#endif
60114

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+
61122
#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

Comments
 (0)