|
| 1 | +#imports |
| 2 | +import time |
| 3 | +import math |
| 4 | + |
| 5 | +#parameters |
| 6 | +seed = int(round(time.time() * 1000)) |
| 7 | +multiplier = 25214903917 |
| 8 | +increment = 11 |
| 9 | +modulus = pow(2, 48) |
| 10 | + |
| 11 | +# a float is a number with decimals |
| 12 | +# an integer is a whole number |
| 13 | +# a boolean is true or false |
| 14 | + |
| 15 | +def randomInt(): # range [0 to 2^48] |
| 16 | + global seed |
| 17 | + seed = (seed * multiplier + increment) % modulus |
| 18 | + return seed |
| 19 | + |
| 20 | +def randomFloat(): # range [0 to 1] |
| 21 | + randomfloat = (randomInt() / modulus) |
| 22 | + return(randomfloat) |
| 23 | + |
| 24 | +def randomIntRange(min, max): # custom range |
| 25 | + return math.floor(randomFloatRange(min, max)) |
| 26 | + |
| 27 | +def randomFloatRange(min, max): # custom range |
| 28 | + return (min + randomFloat() * (max - min)) |
| 29 | + |
| 30 | +def randomBool(chance): #chance of getting true |
| 31 | + if(chance == 0): |
| 32 | + chance = 0.5; |
| 33 | + return randomFloat() < chance; |
| 34 | + |
| 35 | + |
| 36 | +#execute functions |
| 37 | +print("Sequence of Random Integers between 0 and the Modulus") |
| 38 | +print(randomInt()) |
| 39 | +print(randomInt()) |
| 40 | +print(randomInt()) |
| 41 | +print(randomInt()) |
| 42 | +print("\nSequence of Random Floats between 0 and 1") |
| 43 | +print(randomFloat()) |
| 44 | +print(randomFloat()) |
| 45 | +print(randomFloat()) |
| 46 | +print(randomFloat()) |
| 47 | +print("\nSequence of Random Integers between 100 and 250") |
| 48 | +print(randomIntRange(100, 250)) |
| 49 | +print(randomIntRange(100, 250)) |
| 50 | +print(randomIntRange(100, 250)) |
| 51 | +print(randomIntRange(100, 250)) |
| 52 | +print("\nSequence of Random Floats between 60 and 70") |
| 53 | +print(randomFloatRange(60, 70)) |
| 54 | +print(randomFloatRange(60, 70)) |
| 55 | +print(randomFloatRange(60, 70)) |
| 56 | +print(randomFloatRange(60, 70)) |
| 57 | +print("\nSequence of Random Booleans with a chance of 30% of getting true") |
| 58 | +print(randomBool(0.3)) |
| 59 | +print(randomBool(0.3)) |
| 60 | +print(randomBool(0.3)) |
| 61 | +print(randomBool(0.3)) |
0 commit comments