1999-08-30 04:12:12 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP version 4.0 |
|
|
|
|
+----------------------------------------------------------------------+
|
2000-01-01 09:32:05 +08:00
|
|
|
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
|
1999-08-30 04:12:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-02-20 07:41:32 +08:00
|
|
|
| This source file is subject to version 2.01 of the PHP license, |
|
1999-08-30 04:12:12 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-02-20 07:41:32 +08:00
|
|
|
| http://www.php.net/license/2_01.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. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Sascha Schumann <ss@2ns.de> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_lcg.h"
|
|
|
|
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ZTS
|
|
|
|
int lcg_globals_id;
|
|
|
|
#else
|
|
|
|
static php_lcg_globals lcg_globals;
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
* combinedLCG() returns a pseudo random number in the range of (0,1).
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MODMULT(a,b,c,m,s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m
|
|
|
|
|
|
|
|
double php_combined_lcg(void)
|
|
|
|
{
|
|
|
|
long q;
|
|
|
|
long z;
|
|
|
|
LCGLS_FETCH();
|
|
|
|
|
|
|
|
MODMULT(53668,40014,12211,2147483563L, LCG(s1));
|
|
|
|
MODMULT(52774,40692,3791, 2147483399L, LCG(s2));
|
|
|
|
|
|
|
|
z = LCG(s1) - LCG(s2);
|
|
|
|
if(z < 1) {
|
|
|
|
z += 2147483562;
|
|
|
|
}
|
|
|
|
|
|
|
|
return z * 4.656613e-10;
|
|
|
|
}
|
|
|
|
|
1999-12-05 21:41:42 +08:00
|
|
|
static void lcg_init_globals(LCGLS_D)
|
1999-08-30 04:12:12 +08:00
|
|
|
{
|
|
|
|
LCG(s1) = 1;
|
|
|
|
#ifdef ZTS
|
|
|
|
LCG(s2) = (long) tsrm_thread_id();
|
|
|
|
#else
|
|
|
|
LCG(s2) = (long) getpid();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-03-07 04:37:11 +08:00
|
|
|
PHP_MINIT_FUNCTION(lcg)
|
1999-08-30 04:12:12 +08:00
|
|
|
{
|
|
|
|
#ifdef ZTS
|
1999-12-06 00:25:32 +08:00
|
|
|
lcg_globals_id = ts_allocate_id(sizeof(php_lcg_globals), (ts_allocate_ctor) lcg_init_globals, NULL);
|
1999-08-30 04:12:12 +08:00
|
|
|
#else
|
1999-12-05 21:41:42 +08:00
|
|
|
lcg_init_globals();
|
1999-08-30 04:12:12 +08:00
|
|
|
#endif
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* {{{ proto double 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)
|
|
|
|
{
|
|
|
|
RETURN_DOUBLE(php_combined_lcg());
|
|
|
|
}
|
|
|
|
/* }}} */
|