-
Notifications
You must be signed in to change notification settings - Fork 8
/
lcg.py
27 lines (23 loc) · 794 Bytes
/
lcg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np
class LCG:
def __init__(self, seed = 1, Multiplier = 1366, Addend = 150889, Pmod = 714025):
"""Create an LCG instance"""
self.multiplier = Multiplier
self.addend = Addend
self.pmod = Pmod
self.setseed(seed)
def setseed(self, seed):
self.last = seed
def random(self):
"""Return a single random number between 0 and 1"""
self.last = (self.multiplier * self.last + self.addend) % self.pmod
return self.last/self.pmod
def randv(self, size = 1):
"""Return a numpy array of random numbers between 0 and 1"""
v = np.empty(size)
for i in range(size):
v[i] = self.random()
return v
lcgdef = LCG()
print(lcgdef.random())
print(lcgdef.randv(10))