-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlprandom.py
80 lines (67 loc) · 2.01 KB
/
lprandom.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
DESIGN BY CONTRACT EXPECTATION
This is done to make program faster by reducing "if checks"
because I am expecting the user to act according to the following contract
|||| SEED CANNOT BE 0 OR 1 and should be between 0 and 1 ||||
'''
import numpy as np
import time
from numba import jit
R_MAX = 4
R_MIN = 3.9
X_MIN = 0
X_MAX = 1
FORBIDDEN_NUMBER = np.array([.25,.5,.75])#WILL BRING PERIODICITY
def GetTimeSeed():
'''
Returns number between 0 and 1 thats basically epoch time.
'''
return time.time_ns()/100 % 1000 / 1000
def ScaleR(val,rmin = R_MIN,rmax = R_MAX):
'''
DESIGN BY CONTRACT
Only works for value in [0,1]
Not putting any condition or conversion cuz it will slow down the code
'''
return rmin + val*(rmax - rmin)
def ScaleX(val,xmin = X_MIN,xmax = X_MAX):
'''
DESIGN BY CONTRACT
Only works for value in [0,1]
Not putting any condition or conversion cuz it will slow down the code
'''
return xmin + val*(xmax - xmin)
def logistic(r, x):
return r * x * (1 - x)
def normalizer(x):
return np.arccos(1-2*x)/np.pi
@jit(nopython=True)
def run(n, initial, r):
x = [r * initial * (1 - initial)]
for _ in range(n+1000):
x.append(r * x[-1] * (1 - x[-1]))
return np.array(x[1001:])
def lprandom_real(n,seed = None):
run(1,.2,4) #warming up njit
if seed is None:
seed = ScaleX(GetTimeSeed())
if seed in FORBIDDEN_NUMBER:
seed = seed + 0.001
return normalizer(run(n,seed,R_MAX)),seed
def lprandom_real_un(n,seed = None):
run(1,.2,4) #warming up njit
if seed is None:
seed = ScaleX(GetTimeSeed())
if seed in FORBIDDEN_NUMBER:
seed = seed + 0.001
return run(n,seed,R_MAX),seed
def rik(x):
return (x * 256).astype(int)
#for image encryption
def lprandom_int_0_256(n,seed = None):
run(1,.2,4) #warming up njit
if seed is None:
seed = ScaleX(GetTimeSeed())
if seed in FORBIDDEN_NUMBER:
seed = seed + 0.001
return rik(normalizer(run(n,seed,R_MAX))),seed