-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear_congruential_generator.py
60 lines (51 loc) · 1.71 KB
/
linear_congruential_generator.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
# imports
import time;
import math;
# parameters
seed = int(round(time.time() * 1000));
multiplier = 25214903917;
increment = 11;
modulus = pow(2, 48);
# a float is a number with decimals
# an integer is a whole number
# a boolean is true or false
def randomInt(): # range [0 to 2^48]
global seed;
seed = (seed * multiplier + increment) % modulus;
return seed;
def randomFloat(): # range [0 to 1]
return (randomInt() / modulus);
def randomIntRange(min, max): # custom range [minimum, maximum]
return (math.floor(randomFloatRange(min, max)));
def randomFloatRange(min, max): # custom range [minimum, maximum]
return (min + randomFloat() * (max - min));
def randomBool(chance): # chance of getting true (example: randomBool(20) has a 20% chance of returning true)
if(chance == 0):
chance = 0.5;
return (randomFloat() < chance);
# execute functions and print results
print("Sequence of Random Integers between 0 and the Modulus");
print(randomInt());
print(randomInt());
print(randomInt());
print(randomInt());
print("\nSequence of Random Floats between 0 and 1");
print(randomFloat());
print(randomFloat());
print(randomFloat());
print(randomFloat());
print("\nSequence of Random Integers between 100 and 250");
print(randomIntRange(100, 250));
print(randomIntRange(100, 250));
print(randomIntRange(100, 250));
print(randomIntRange(100, 250));
print("\nSequence of Random Floats between 60 and 70");
print(randomFloatRange(60, 70));
print(randomFloatRange(60, 70));
print(randomFloatRange(60, 70));
print(randomFloatRange(60, 70));
print("\nSequence of Random Booleans with a chance of 30% of getting true");
print(randomBool(0.3));
print(randomBool(0.3));
print(randomBool(0.3));
print(randomBool(0.3));