2001-05-07 01:55:10 +08:00
|
|
|
/*
|
1999-08-30 04:12:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
1999-08-30 04:12:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| Copyright (c) 1997-2004 The PHP Group |
|
1999-08-30 04:12:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-06-11 04:04:29 +08:00
|
|
|
| This source file is subject to version 3.0 of the PHP license, |
|
1999-08-30 04:12:12 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_0.txt. |
|
1999-08-30 04:12:12 +08:00
|
|
|
| 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. |
|
|
|
|
+----------------------------------------------------------------------+
|
2002-02-28 16:29:35 +08:00
|
|
|
| Author: Sascha Schumann <sascha@schumann.cx> |
|
1999-08-30 04:12:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-07-24 09:40:02 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
1999-08-30 04:12:12 +08:00
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_lcg.h"
|
|
|
|
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-22 01:21:50 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
#include "win32/time.h"
|
2002-09-06 15:27:27 +08:00
|
|
|
#elif defined(NETWARE)
|
|
|
|
#ifdef NEW_LIBC
|
|
|
|
#include <sys/timeval.h>
|
|
|
|
#else
|
|
|
|
#include "netware/time_nw.h"
|
|
|
|
#endif
|
2001-09-22 01:21:50 +08:00
|
|
|
#else
|
2001-09-21 21:38:44 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
1999-08-30 04:12:12 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
int lcg_globals_id;
|
|
|
|
#else
|
|
|
|
static php_lcg_globals lcg_globals;
|
2001-08-08 03:21:19 +08:00
|
|
|
#endif
|
|
|
|
|
2001-05-07 00:54:27 +08:00
|
|
|
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef PHP_WIN32
|
1999-09-22 13:26:22 +08:00
|
|
|
#include <process.h>
|
|
|
|
#endif
|
|
|
|
|
1999-08-30 04:12:12 +08:00
|
|
|
/*
|
2001-08-12 01:03:37 +08:00
|
|
|
* combinedLCG() returns a pseudo random number in the range of (0, 1).
|
1999-08-30 04:12:12 +08:00
|
|
|
* The function combines two CGs with periods of
|
|
|
|
* 2^31 - 85 and 2^31 - 249. The period of this function
|
|
|
|
* is equal to the product of both primes.
|
|
|
|
*/
|
|
|
|
|
2001-08-12 01:03:37 +08:00
|
|
|
#define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m
|
1999-08-30 04:12:12 +08:00
|
|
|
|
2003-03-31 13:39:25 +08:00
|
|
|
static void lcg_seed(TSRMLS_D);
|
|
|
|
|
2001-09-21 21:38:44 +08:00
|
|
|
PHPAPI double php_combined_lcg(TSRMLS_D)
|
1999-08-30 04:12:12 +08:00
|
|
|
{
|
2001-07-19 06:55:52 +08:00
|
|
|
php_int32 q;
|
|
|
|
php_int32 z;
|
2003-03-31 11:24:18 +08:00
|
|
|
|
|
|
|
if (!LCG(seeded)) {
|
|
|
|
lcg_seed(TSRMLS_C);
|
|
|
|
}
|
1999-08-30 04:12:12 +08:00
|
|
|
|
2001-08-12 01:03:37 +08:00
|
|
|
MODMULT(53668, 40014, 12211, 2147483563L, LCG(s1));
|
|
|
|
MODMULT(52774, 40692, 3791, 2147483399L, LCG(s2));
|
1999-08-30 04:12:12 +08:00
|
|
|
|
|
|
|
z = LCG(s1) - LCG(s2);
|
2001-09-21 21:38:44 +08:00
|
|
|
if (z < 1) {
|
1999-08-30 04:12:12 +08:00
|
|
|
z += 2147483562;
|
|
|
|
}
|
|
|
|
|
|
|
|
return z * 4.656613e-10;
|
|
|
|
}
|
|
|
|
|
2001-09-21 21:38:44 +08:00
|
|
|
static void lcg_seed(TSRMLS_D)
|
1999-08-30 04:12:12 +08:00
|
|
|
{
|
2001-09-21 21:38:44 +08:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, NULL) == 0) {
|
|
|
|
LCG(s1) = tv.tv_sec ^ (~tv.tv_usec);
|
|
|
|
} else {
|
|
|
|
LCG(s1) = 1;
|
|
|
|
}
|
1999-08-30 04:12:12 +08:00
|
|
|
#ifdef ZTS
|
|
|
|
LCG(s2) = (long) tsrm_thread_id();
|
|
|
|
#else
|
|
|
|
LCG(s2) = (long) getpid();
|
|
|
|
#endif
|
2001-09-21 21:38:44 +08:00
|
|
|
|
|
|
|
LCG(seeded) = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lcg_init_globals(php_lcg_globals *lcg_globals_p TSRMLS_DC)
|
|
|
|
{
|
|
|
|
LCG(seeded) = 0;
|
1999-08-30 04:12:12 +08:00
|
|
|
}
|
|
|
|
|
2001-05-07 01:55:10 +08:00
|
|
|
PHP_MINIT_FUNCTION(lcg)
|
|
|
|
{
|
2001-09-21 21:38:44 +08:00
|
|
|
#ifdef ZTS
|
2001-07-27 18:16:41 +08:00
|
|
|
ts_allocate_id(&lcg_globals_id, sizeof(php_lcg_globals), (ts_allocate_ctor) lcg_init_globals, NULL);
|
2001-09-21 21:38:44 +08:00
|
|
|
#else
|
|
|
|
lcg_init_globals(&lcg_globals);
|
|
|
|
#endif
|
2001-05-07 01:55:10 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-09-21 21:38:44 +08:00
|
|
|
|
2001-05-07 00:54:27 +08:00
|
|
|
PHP_RINIT_FUNCTION(lcg)
|
1999-08-30 04:12:12 +08:00
|
|
|
{
|
2001-09-21 21:38:44 +08:00
|
|
|
if (!LCG(seeded)) {
|
|
|
|
lcg_seed(TSRMLS_C);
|
2001-05-07 00:54:27 +08:00
|
|
|
}
|
1999-08-30 04:12:12 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
/* {{{ proto float lcg_value()
|
2000-02-24 16:07:29 +08:00
|
|
|
Returns a value from the combined linear congruential generator */
|
1999-08-30 04:12:12 +08:00
|
|
|
PHP_FUNCTION(lcg_value)
|
|
|
|
{
|
2001-08-06 11:50:52 +08:00
|
|
|
RETURN_DOUBLE(php_combined_lcg(TSRMLS_C));
|
1999-08-30 04:12:12 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2001-06-06 21:06:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
2001-06-06 21:06:12 +08:00
|
|
|
*/
|