Skip to content

Commit 54ce056

Browse files
committed
small changes
1 parent 55c6386 commit 54ce056

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# LCG Example Python
2-
a linear congruential generator example in python
1+
# LCG Example Python:
2+
3+
a linear congruential generator example in python. seed is generated with the current time
4+
5+
## Video about this Topic:
6+
7+
Work in progress...
8+
9+
## Functions:
10+
11+
- randomInt() - generates random Integer between '0' and '2^48'
12+
- randomFloat() - generates random Float between '0' and '1'
13+
- randomIntRange(min, max) - generates random Integer between variables 'min' and 'max'
14+
- randomFloatRange(min, max) - generates random Float between variables 'min' and 'max'
15+
- randomBool(Chance) - generates random Boolean with the chance of getting true being 'chance' (Default: 0.5)

linear_congruential_generator.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11
# imports
2-
import time
3-
import math
2+
import time;
3+
import math;
44

55
# parameters
6-
seed = int(round(time.time() * 1000))
7-
multiplier = 25214903917
8-
increment = 11
9-
modulus = pow(2, 48)
6+
seed = int(round(time.time() * 1000));
7+
multiplier = 25214903917;
8+
increment = 11;
9+
modulus = pow(2, 48);
1010

1111
# a float is a number with decimals
1212
# an integer is a whole number
1313
# a boolean is true or false
1414

1515
def randomInt(): # range [0 to 2^48]
16-
global seed
17-
seed = (seed * multiplier + increment) % modulus
18-
return seed
16+
global seed;
17+
seed = (seed * multiplier + increment) % modulus;
18+
return seed;
1919

2020
def randomFloat(): # range [0 to 1]
21-
return (randomInt() / modulus)
21+
return (randomInt() / modulus);
2222

2323
def randomIntRange(min, max): # custom range [minimum, maximum]
24-
return (math.floor(randomFloatRange(min, max)))
24+
return (math.floor(randomFloatRange(min, max)));
2525

2626
def randomFloatRange(min, max): # custom range [minimum, maximum]
27-
return (min + randomFloat() * (max - min))
27+
return (min + randomFloat() * (max - min));
2828

2929
def randomBool(chance): # chance of getting true (example: randomBool(20) has a 20% chance of returning true)
3030
if(chance == 0):
31-
chance = 0.5
32-
return (randomFloat() < chance)
31+
chance = 0.5;
32+
return (randomFloat() < chance);
3333

3434

3535
# execute functions and print results
36-
print("Sequence of Random Integers between 0 and the Modulus")
37-
print(randomInt())
38-
print(randomInt())
39-
print(randomInt())
40-
print(randomInt())
41-
print("\nSequence of Random Floats between 0 and 1")
42-
print(randomFloat())
43-
print(randomFloat())
44-
print(randomFloat())
45-
print(randomFloat())
46-
print("\nSequence of Random Integers between 100 and 250")
47-
print(randomIntRange(100, 250))
48-
print(randomIntRange(100, 250))
49-
print(randomIntRange(100, 250))
50-
print(randomIntRange(100, 250))
51-
print("\nSequence of Random Floats between 60 and 70")
52-
print(randomFloatRange(60, 70))
53-
print(randomFloatRange(60, 70))
54-
print(randomFloatRange(60, 70))
55-
print(randomFloatRange(60, 70))
56-
print("\nSequence of Random Booleans with a chance of 30% of getting true")
57-
print(randomBool(0.3))
58-
print(randomBool(0.3))
59-
print(randomBool(0.3))
60-
print(randomBool(0.3))
36+
print("Sequence of Random Integers between 0 and the Modulus");
37+
print(randomInt());
38+
print(randomInt());
39+
print(randomInt());
40+
print(randomInt());
41+
print("\nSequence of Random Floats between 0 and 1");
42+
print(randomFloat());
43+
print(randomFloat());
44+
print(randomFloat());
45+
print(randomFloat());
46+
print("\nSequence of Random Integers between 100 and 250");
47+
print(randomIntRange(100, 250));
48+
print(randomIntRange(100, 250));
49+
print(randomIntRange(100, 250));
50+
print(randomIntRange(100, 250));
51+
print("\nSequence of Random Floats between 60 and 70");
52+
print(randomFloatRange(60, 70));
53+
print(randomFloatRange(60, 70));
54+
print(randomFloatRange(60, 70));
55+
print(randomFloatRange(60, 70));
56+
print("\nSequence of Random Booleans with a chance of 30% of getting true");
57+
print(randomBool(0.3));
58+
print(randomBool(0.3));
59+
print(randomBool(0.3));
60+
print(randomBool(0.3));

0 commit comments

Comments
 (0)