1
|
|
|
""" |
2
|
|
|
Random module docs. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from __future__ import absolute_import as _ |
6
|
|
|
|
7
|
|
|
import time |
8
|
|
|
|
9
|
|
|
from tcod.libtcod import ffi, lib |
10
|
|
|
from tcod.libtcod import RNG_MT as MERSENNE_TWISTER |
|
|
|
|
11
|
|
|
from tcod.libtcod import RNG_CMWC as COMPLEMENTARY_MULTIPLY_WITH_CARRY |
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class Random(object): |
15
|
|
|
"""The libtcod random number generator. |
16
|
|
|
|
17
|
|
|
If all you need is a random number generator then it's recommended |
18
|
|
|
that you use the :any:`random` module from the Python standard library. |
19
|
|
|
|
20
|
|
|
If ``seed`` is None then a random seed will be generated. |
21
|
|
|
|
22
|
|
|
Args: |
23
|
|
|
algorithm (int): The algorithm to use. |
24
|
|
|
seed (Optional[Hashable]): |
25
|
|
|
Could be a 32-bit integer, but any hashable object is accepted. |
26
|
|
|
|
27
|
|
|
Attributes: |
28
|
|
|
random_c (CData): A cffi pointer to a TCOD_random_t object. |
29
|
|
|
""" |
30
|
|
|
def __init__(self, algorithm, seed=None): |
31
|
|
|
"""Create a new instance using this algorithm and seed.""" |
32
|
|
|
if seed is None: |
33
|
|
|
seed = time.time() + time.clock() |
34
|
|
|
self.random_c = ffi.gc( |
35
|
|
|
ffi.cast('mersenne_data_t*', |
36
|
|
|
lib.TCOD_random_new_from_seed(algorithm, |
37
|
|
|
hash(seed) % (1 << 32))), |
38
|
|
|
lib.TCOD_random_delete) |
39
|
|
|
|
40
|
|
|
@classmethod |
41
|
|
|
def _new_from_cdata(cls, cdata): |
42
|
|
|
"""Return a new instance encapsulating this cdata.""" |
43
|
|
|
self = object.__new__(cls) |
44
|
|
|
self.random_c = cdata |
45
|
|
|
return self |
46
|
|
|
|
47
|
|
|
def randint(self, low, high): |
48
|
|
|
"""Return a random integer within the linear range: low <= n <= high. |
49
|
|
|
|
50
|
|
|
Args: |
51
|
|
|
low (int): The lower bound of the random range. |
52
|
|
|
high (int): The upper bound of the random range. |
53
|
|
|
|
54
|
|
|
Returns: |
55
|
|
|
int: A random integer. |
56
|
|
|
""" |
57
|
|
|
return lib.TCOD_random_get_i(self.random_c, low, high) |
58
|
|
|
|
59
|
|
|
def uniform(self, low, high): |
60
|
|
|
"""Return a random floating number in the range: low <= n <= high. |
61
|
|
|
|
62
|
|
|
Args: |
63
|
|
|
low (int): The lower bound of the random range. |
64
|
|
|
high (int): The upper bound of the random range. |
65
|
|
|
|
66
|
|
|
Returns: |
67
|
|
|
float: A random float. |
68
|
|
|
""" |
69
|
|
|
return lib.TCOD_random_get_double(self.random_c, low, high) |
70
|
|
|
|
71
|
|
|
def guass(self, mu, sigma): |
|
|
|
|
72
|
|
|
"""Return a random number using Gaussian distribution. |
73
|
|
|
|
74
|
|
|
Args: |
75
|
|
|
mu (float): The median returned value. |
76
|
|
|
sigma (float): The standard deviation. |
77
|
|
|
|
78
|
|
|
Returns: |
79
|
|
|
float: A random float. |
80
|
|
|
""" |
81
|
|
|
return lib.TCOD_random_get_gaussian_double(self.random_c, mu, sigma) |
82
|
|
|
|
83
|
|
|
def inverse_guass(self, mu, sigma): |
|
|
|
|
84
|
|
|
"""Return a random Gaussian number using the Box-Muller transform. |
85
|
|
|
|
86
|
|
|
Args: |
87
|
|
|
mu (float): The median returned value. |
88
|
|
|
sigma (float): The standard deviation. |
89
|
|
|
|
90
|
|
|
Returns: |
91
|
|
|
float: A random float. |
92
|
|
|
""" |
93
|
|
|
return lib.TCOD_random_get_gaussian_double_inv(self.random_c, mu, sigma) |
94
|
|
|
|
95
|
|
|
def __getstate__(self): |
96
|
|
|
"""Pack the self.random_c attribute into a portable state.""" |
97
|
|
|
state = self.__dict__.copy() |
98
|
|
|
state['random_c'] = { |
99
|
|
|
'algo': self.random_c.algo, |
100
|
|
|
'distribution': self.random_c.distribution, |
101
|
|
|
'mt': list(self.random_c.mt), |
102
|
|
|
'cur_mt': self.random_c.cur_mt, |
103
|
|
|
'Q': list(self.random_c.Q), |
104
|
|
|
'c': self.random_c.c, |
105
|
|
|
'cur': self.random_c.cur, |
106
|
|
|
} |
107
|
|
|
return state |
108
|
|
|
|
109
|
|
|
def __setstate__(self, state): |
110
|
|
|
"""Create a new cdata object with the stored paramaters.""" |
111
|
|
|
try: |
112
|
|
|
cdata = state['random_c'] |
113
|
|
|
except KeyError: # old/deprecated format |
114
|
|
|
cdata = state['cdata'] |
115
|
|
|
del state['cdata'] |
116
|
|
|
state['random_c'] = ffi.new('mersenne_data_t*', cdata) |
117
|
|
|
self.__dict__.update(state) |
118
|
|
|
|