Skip to content

Commit e71b4bf

Browse files
authored
Merge pull request #19 from schmouk/augment-operator-call
#16-Augment operator '()' This is part #2/2 of the delivery for Release 1.2.0.
2 parents 72c6def + fac0db2 commit e71b4bf

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed

PyRandLib/README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,44 @@ Note 3: a Cython version of PyRandLib might be delivered in a next release.
123123
Up today, no date is planned for this.
124124

125125

126+
## New in release 1.2
127+
This is available starting at version 1.2 of PyRandLib. The call operator
128+
(i.e., '()') gets a new signature which is still backward compatible with
129+
previous versions of this library. Its new use is described here below. The
130+
implementation code can be found in class `BaseRandom`, in module
131+
`baserandom.py`.
132+
133+
from fastrand63 import FastRand63
134+
135+
rand = FastRand63()
136+
137+
# prints a float random value ranging in [0.0, 1.0]
138+
print( rand() )
139+
140+
# prints an integer random value ranging in [0, 5]
141+
print( rand(5) )
142+
143+
# prints a float random value ranging in [0.0, 20.0]
144+
print( rand(20.0)
145+
146+
# prints a list of 10 integer values each ranging in [0, 5]
147+
print( rand(5, 10) )
148+
149+
# prints a list of 10 float values each ranging in [0.0, 1.0]
150+
print( rand(times=10) )
151+
152+
# prints a list of 4 random values ranging respectively in
153+
# [0, 5], [0.0, 50.0], [0.0, 500.0] and [0, 5000]
154+
print( rand(5, 50.0, 500.0, 5000) )
155+
156+
# a more complex call which prints something like:
157+
# [ [3, 11.64307079016269, 127.65395855782158, 4206, [2, 0, 1, 4, 4, 1, 2, 0]],
158+
# [2, 34.22526698212995, 242.54183578253426, 2204, [5, 3, 5, 4, 2, 0, 1, 3]],
159+
# [0, 17.77303802057933, 417.70662295909983, 559, [4, 1, 5, 0, 5, 3, 0, 5]] ]
160+
print( rand( (5, 50.0, 500.0, 5000, [5]*8), times=3 ) )
161+
162+
163+
126164
## Architecture overview
127165
Each of the implemented PRG is described in an independent module. The name
128166
of the module is directly related to the name of the related class.
@@ -136,7 +174,7 @@ aims at providing simple common behavior for all PRG classes of the library,
136174
the most noticeable one being the 'callable' nature of every implemented
137175
PRGs. For instance:
138176

139-
rand = UBaseRandom()
177+
rand = BaseRandom()
140178
print( rand() ) # prints a uniform pseudo-random value within [0.0, 1.0)
141179
print( rand(a) ) # prints a uniform pseudo-random value within [0.0, a)
142180
print( rand(a,b) ) # prints a uniform pseudo-random value within [a, b)

PyRandLib/baserandom.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
#=============================================================================
2626
from random import Random
27-
from typing import Any
27+
from typing import Any, List, Tuple, Union
2828

29-
from .types import Numeric, SeedStateType
29+
from .types import Numerical, SeedStateType, StateType
3030

3131

3232
#=============================================================================
@@ -243,12 +243,39 @@ def seed(self, _seed: SeedStateType = None) -> None:
243243

244244

245245
#------------------------------------------------------------------------=
246-
def __call__(self, _max: Numeric = 1.0) -> float:
246+
def __call__(self, _max : Union[Numerical,
247+
Tuple[Numerical],
248+
List[Numerical]] = 1.0,
249+
times: int = 1 ) -> Numerical:
247250
"""This class's instances are callable.
248251
249-
The returned value is uniformly contained
250-
within the interval [0.0 : _max].
252+
The returned value is uniformly contained within the
253+
interval [0.0 : _max]. When times is set, a list of
254+
iterated pseudo-random values is returned. 'times'
255+
must be an integer. If less than 1 it is forced to
256+
be 1.
257+
'_max' may be a list or a tuple of values, in which
258+
case a list of related pseudo-random values is
259+
returned with entries of the same type than the same
260+
indexed entry in '_max'.
251261
"""
252-
return self.uniform( 0.0, _max )
262+
assert isinstance( times, int )
263+
if times < 1:
264+
times = 1
265+
266+
if isinstance( _max, int ):
267+
ret = [ self.randint(0, _max) for _ in range(times) ]
268+
elif isinstance( _max, float ):
269+
ret = [ self.uniform( 0.0, _max ) for _ in range(times) ]
270+
else:
271+
try:
272+
if times == 1:
273+
ret = [ self(m,1) for m in _max]
274+
else:
275+
ret = [ [self(m,1) for m in _max] for _ in range(times) ]
276+
except:
277+
ret = [ self.__call__(times=1) ]
278+
279+
return ret[0] if len(ret) == 1 else ret
253280

254281
#===== end of module baserandom.py =====================================

PyRandLib/fastrand32.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import time
2727

2828
from .baselcg import BaseLCG
29-
from .types import Numeric
29+
from .types import Numerical
3030

3131

3232
#=============================================================================
@@ -93,10 +93,10 @@ class FastRand32( BaseLCG ):
9393
"""
9494

9595
#------------------------------------------------------------------------=
96-
def __init__(self, _seed: Numeric = None) -> None:
96+
def __init__(self, _seed: Numerical = None) -> None:
9797
"""Constructor.
9898
99-
Should _seed be None or not a numeric then the local
99+
Should _seed be None or not a numerical then the local
100100
time is used (with its shuffled value) as a seed.
101101
"""
102102
super().__init__( _seed ) # this call creates attribute self._value and sets it
@@ -113,7 +113,7 @@ def random(self) -> float:
113113

114114

115115
#------------------------------------------------------------------------=
116-
def setstate(self, _state: Numeric) -> None:
116+
def setstate(self, _state: Numerical) -> None:
117117
"""Restores the internal state of the generator.
118118
119119
_state should have been obtained from a previous call

PyRandLib/fastrand63.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import time
2727

2828
from .baselcg import BaseLCG
29-
from .types import Numeric
29+
from .types import Numerical
3030

3131

3232
#=============================================================================
@@ -103,7 +103,7 @@ def random(self) -> float:
103103

104104

105105
#------------------------------------------------------------------------=
106-
def setstate(self, _state: Numeric) -> None:
106+
def setstate(self, _state: Numerical) -> None:
107107
"""Restores the internal state of the generator.
108108
109109
_state should have been obtained from a previous call

PyRandLib/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
#=============================================================================
2626
from typing import List, Tuple, Union
2727

28-
Numeric = Union[ int, float ]
29-
StateType = Union[ Tuple[Numeric], List[Numeric], Tuple[List[Numeric], int] ]
30-
SeedStateType = Union[ Numeric, StateType ]
28+
Numerical = Union[ int, float ]
29+
StateType = Union[ Tuple[Numerical], List[Numerical], Tuple[List[Numerical], int] ]
30+
SeedStateType = Union[ Numerical, StateType ]
3131

3232

3333
#===== end of PyRandLib.types =====#

0 commit comments

Comments
 (0)