php-src/ext/standard/lcg.c

133 lines
3.1 KiB
C
Raw Normal View History

/*
1999-08-29 20:12:12 +00:00
+----------------------------------------------------------------------+
2014-09-19 16:33:14 +00:00
| PHP Version 7 |
1999-08-29 20:12:12 +00:00
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
1999-08-29 20:12:12 +00:00
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
1999-08-29 20:12:12 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
1999-08-29 20:12:12 +00: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 08:29:35 +00:00
| Author: Sascha Schumann <sascha@schumann.cx> |
1999-08-29 20:12:12 +00:00
+----------------------------------------------------------------------+
*/
/* $Id$ */
1999-08-29 20:12:12 +00:00
#include "php.h"
#include "php_lcg.h"
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef PHP_WIN32
#include "win32/time.h"
#else
#include <sys/time.h>
#endif
1999-08-29 20:12:12 +00:00
#ifdef ZTS
2014-09-25 16:48:27 +00:00
int lcg_globals_id;
1999-08-29 20:12:12 +00:00
#else
static php_lcg_globals lcg_globals;
2001-08-07 19:21:19 +00:00
#endif
2000-02-11 15:59:30 +00:00
#ifdef PHP_WIN32
#include <process.h>
#endif
1999-08-29 20:12:12 +00:00
/*
2001-08-11 17:03:37 +00:00
* combinedLCG() returns a pseudo random number in the range of (0, 1).
2007-11-06 10:57:10 +00:00
* The function combines two CGs with periods of
1999-08-29 20:12:12 +00:00
* 2^31 - 85 and 2^31 - 249. The period of this function
* is equal to the product of both primes.
*/
2001-08-11 17:03:37 +00: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-29 20:12:12 +00:00
2014-12-13 22:06:14 +00:00
static void lcg_seed(void);
2014-12-13 22:06:14 +00:00
PHPAPI double php_combined_lcg(void) /* {{{ */
1999-08-29 20:12:12 +00:00
{
php_int32 q;
php_int32 z;
2007-11-06 10:57:10 +00:00
if (!LCG(seeded)) {
2014-12-13 22:06:14 +00:00
lcg_seed();
}
1999-08-29 20:12:12 +00:00
2001-08-11 17:03:37 +00:00
MODMULT(53668, 40014, 12211, 2147483563L, LCG(s1));
MODMULT(52774, 40692, 3791, 2147483399L, LCG(s2));
1999-08-29 20:12:12 +00:00
z = LCG(s1) - LCG(s2);
if (z < 1) {
1999-08-29 20:12:12 +00:00
z += 2147483562;
}
return z * 4.656613e-10;
}
2007-11-06 10:57:10 +00:00
/* }}} */
1999-08-29 20:12:12 +00:00
2014-12-13 22:06:14 +00:00
static void lcg_seed(void) /* {{{ */
1999-08-29 20:12:12 +00:00
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == 0) {
LCG(s1) = tv.tv_sec ^ (tv.tv_usec<<11);
} else {
LCG(s1) = 1;
}
1999-08-29 20:12:12 +00:00
#ifdef ZTS
2014-08-26 09:23:25 +00:00
LCG(s2) = (zend_long) tsrm_thread_id();
1999-08-29 20:12:12 +00:00
#else
2014-08-26 09:23:25 +00:00
LCG(s2) = (zend_long) getpid();
1999-08-29 20:12:12 +00:00
#endif
/* Add entropy to s2 by calling gettimeofday() again */
if (gettimeofday(&tv, NULL) == 0) {
LCG(s2) ^= (tv.tv_usec<<11);
}
LCG(seeded) = 1;
}
2007-11-06 10:57:10 +00:00
/* }}} */
2014-12-13 22:06:14 +00:00
static void lcg_init_globals(php_lcg_globals *lcg_globals_p) /* {{{ */
{
LCG(seeded) = 0;
1999-08-29 20:12:12 +00:00
}
2007-11-06 10:57:10 +00:00
/* }}} */
1999-08-29 20:12:12 +00:00
2007-11-06 10:57:10 +00:00
PHP_MINIT_FUNCTION(lcg) /* {{{ */
{
#ifdef ZTS
2014-09-25 16:48:27 +00:00
ts_allocate_id(&lcg_globals_id, sizeof(php_lcg_globals), (ts_allocate_ctor) lcg_init_globals, NULL);
#else
lcg_init_globals(&lcg_globals);
#endif
return SUCCESS;
}
2007-11-06 10:57:10 +00:00
/* }}} */
2001-09-21 21:59:27 +00:00
/* {{{ proto float lcg_value()
2000-02-24 08:07:29 +00:00
Returns a value from the combined linear congruential generator */
1999-08-29 20:12:12 +00:00
PHP_FUNCTION(lcg_value)
{
2014-12-13 22:06:14 +00:00
RETURN_DOUBLE(php_combined_lcg());
1999-08-29 20:12:12 +00:00
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/