xref: /PHP-5.6/ext/standard/php_rand.h (revision 49493a2d)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Rasmus Lerdorf <rasmus@php.net>                             |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Pedro Melo <melo@ip.pt>                                     |
18    |          Sterling Hughes <sterling@php.net>                          |
19    |                                                                      |
20    | Based on code from: Shawn Cokus <Cokus@math.washington.edu>          |
21    +----------------------------------------------------------------------+
22  */
23 /* $Id$ */
24 
25 #ifndef PHP_RAND_H
26 #define	PHP_RAND_H
27 
28 #include <stdlib.h>
29 #include "basic_functions.h"
30 #include "php_lcg.h"
31 
32 /* System Rand functions */
33 #ifndef RAND_MAX
34 #define RAND_MAX (1<<15)
35 #endif
36 
37 /* In ZTS mode we rely on rand_r() so we must use RAND_MAX. */
38 #if !defined(ZTS) && (defined(HAVE_LRAND48) || defined(HAVE_RANDOM))
39 #define PHP_RAND_MAX 2147483647
40 #else
41 #define PHP_RAND_MAX RAND_MAX
42 #endif
43 
44 #define RAND_RANGE(__n, __min, __max, __tmax) \
45     (__n) = (__min) + (long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
46 
47 /* MT Rand */
48 #define PHP_MT_RAND_MAX ((long) (0x7FFFFFFF)) /* (1<<31) - 1 */
49 
50 #ifdef PHP_WIN32
51 #define GENERATE_SEED() (((long) (time(0) * GetCurrentProcessId())) ^ ((long) (1000000.0 * php_combined_lcg(TSRMLS_C))))
52 #else
53 #define GENERATE_SEED() (((long) (time(0) * getpid())) ^ ((long) (1000000.0 * php_combined_lcg(TSRMLS_C))))
54 #endif
55 
56 PHPAPI void php_srand(long seed TSRMLS_DC);
57 PHPAPI long php_rand(TSRMLS_D);
58 PHPAPI void php_mt_srand(php_uint32 seed TSRMLS_DC);
59 PHPAPI php_uint32 php_mt_rand(TSRMLS_D);
60 
61 #endif	/* PHP_RAND_H */
62