diff options
author | Jeroen van Wolffelaar <jeroen@php.net> | 2001-09-03 01:06:23 +0000 |
---|---|---|
committer | Jeroen van Wolffelaar <jeroen@php.net> | 2001-09-03 01:06:23 +0000 |
commit | f7306ce0109b246ff23901ed6b91d58de4c9dd42 (patch) | |
tree | ae671af313651abee6fe81dee1f84b5f7ab1ca25 /ext/standard/php_rand.h | |
parent | d4197ea703cef9f1cf6afd7065b4cab9af4a898c (diff) | |
download | php-git-f7306ce0109b246ff23901ed6b91d58de4c9dd42.tar.gz |
Merge RAND_REDESIGN into MAIN
Diffstat (limited to 'ext/standard/php_rand.h')
-rw-r--r-- | ext/standard/php_rand.h | 131 |
1 files changed, 100 insertions, 31 deletions
diff --git a/ext/standard/php_rand.h b/ext/standard/php_rand.h index b4d7e991d0..19686a8066 100644 --- a/ext/standard/php_rand.h +++ b/ext/standard/php_rand.h @@ -15,47 +15,116 @@ | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> | | Zeev Suraski <zeev@zend.com> | | Pedro Melo <melo@ip.pt> | + | Jeroen van Wolffelaar <jeroen@php.net> | | | | Based on code from: Shawn Cokus <Cokus@math.washington.edu> | +----------------------------------------------------------------------+ */ /* $Id$ */ +/* Layout implementation random functions + * + * The PHPAPI contains these functions: + * - long php_rand() + * - long php_rand_range(long min, long max) + * - void php_srand() + * - long php_getrandmax() + * + * Note that it is not possible to choose the algoritm. This is done to + * give the user the possibility to control all randomness by means of + * srand()/php.ini in a portable and consistent way. + * + * rand.c: (the only rand*.c file with PHP_API and PHP_FUNCTION functions) + * + * - PHP_FUNCTION([mt_]srand) + * +-> void php_srand(void) + * +-> void php_srand2(long seed, int alg) + * +-> (rand_sys.c) long php_rand_sys() + * +-> (rand_mt.c ) long php_rand_mt() + * + * - PHP_FUNCTION([mt_]rand) + * +-> long php_rand() + * +-> (rand_sys.c) long php_rand_sys() + * +-> (rand_mt.c ) long php_rand_mt() + * +-> long php_rand_range(long min, long max) + * +-> calls php_rand() + * + * - PHP_FUNCTION([mt_]getrandmax) + * +-> PHPAPI long php_randmax(void) + * +-> (rand_sys.c) long php_randmax_sys() + * +-> (rand_mt.c ) long php_randmax_mt() + * + * --Jeroen + */ + +/* TODO: + * - make constants available to PHP-user + * - MINFO section about which random number generators are available + * - Nuke randmax by enhancing PHP_RAND_RANGE to work well in the case of a + * greater request than the real (internal) randmax is + * - Implement LCG + * - Implement a real-random source? (via internet, and/or /dev/urandom?) + * - Can lrand48 be thread-safe? + * - Is random() useful sometimes? + * - Which system algorithms are available, maybe name them after real + * algorithm by compile-time detection? + * - Get this to compile :-) + */ #ifndef PHP_RAND_H #define PHP_RAND_H #include <stdlib.h> -#ifndef RAND_MAX -#define RAND_MAX (1<<15) -#endif - -#if HAVE_LRAND48 -#define PHP_RAND_MAX 2147483647 -#else -#define PHP_RAND_MAX RAND_MAX -#endif - -/* Define rand Function wrapper */ -#ifdef HAVE_RANDOM -#define php_rand() random() -#else -#ifdef HAVE_LRAND48 -#define php_rand() lrand48() -#else -#define php_rand() rand() -#endif -#endif - -/* Define srand Function wrapper */ -#ifdef HAVE_SRANDOM -#define php_srand(seed) srandom((unsigned int)seed) -#else -#ifdef HAVE_SRAND48 -#define php_srand(seed) srand48((long)seed) -#else -#define php_srand(seed) srand((unsigned int)seed) -#endif -#endif +/* FIXME: that '_php_randgen_entry' needed, or not? */ +typedef struct _php_randgen_entry { + void (*srand)(long seed); + long (*rand)(void); + long randmax; + char *ini_str; +} php_randgen_entry; + +/* an ARRAY of POINTERS, not vice versa */ +extern php_randgen_entry *php_randgen_entries[]; + +#define PHP_RANDGEN_ENTRY(which, nsrand, nrand, nrandmax, nini_str) { \ + php_randgen_entries[which] = emalloc(sizeof(php_randgen_entry)); \ + php_randgen_entries[which]->srand = nsrand; \ + php_randgen_entries[which]->rand = nrand; \ + php_randgen_entries[which]->randmax = nrandmax; \ + php_randgen_entries[which]->ini_str = nini_str; \ +} + +/* Define random generator constants */ +#define PHP_RAND_SYS 0 +#define PHP_RAND_LRAND48 1 +#define PHP_RAND_MT 2 +#define PHP_RAND_LCG 3 + +#define PHP_RAND_DEFAULT PHP_RAND_MT + +/* how many there are */ +#define PHP_RAND_NUMRANDS 4 + +/* Proto's */ +PHP_FUNCTION(srand); +PHP_FUNCTION(rand); +PHP_FUNCTION(getrandmax); +PHP_FUNCTION(mt_srand); +PHP_FUNCTION(mt_rand); +PHP_FUNCTION(mt_getrandmax); + +PHPAPI long php_rand(void); +PHPAPI long php_rand_range(long min, long max); +PHPAPI double php_drand(void); +PHPAPI long php_randmax(void); #endif /* PHP_RAND_H */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: fdm=marker + * vim: sw=4 ts=4 tw=78 + */ |