Discrete Event Simulation in Python
Diogo Costa (PhD, MSc)
Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific
What is randomization?
Worth noting: Computers incapable, by design, of generating truly random numbers.
"Truly" random generation
Pseudo-random generation
Example: Factory with manual assembly task
Several statistics packages can be used:
Random package
Our focus
Random integer between a given range
Method:
random.randint(start, end)
Example: Random integer between 3 (included) and 9 (not included)
random.randint(3, 9)
5
Random float number between a given range
Method:
random.uniform(start, end)
Example: Random float number between 3 and 9
random.uniform(3, 9)
6.4557754
Random sample from a sequence
Method:
random.sample(mylist, k=number_samples)
Example: 2 Random elements from a list
mylist = ["apple", "banana", "cherry"]
random.sample(mylist, k=2)
['cherry', 'banana']
Shuffle a list (re-order list elements)
Method:
random.shuffle(mylist)
Example: Random re-ordering list elements
mylist = ["book", "pencil", "eraser"]
random.shuffle(mylist)
['eraser', 'pencil', 'book']
Gaussian distribution
Method:
random.gauss(average, standard_dev)
Example: Pseudo-random float with mean of 100 and standard deviation of 50
random.gauss(100, 50)
123.59383
Exponential distribution
Method:
random.expovariate(lambda)
lambda>0
, results [0, infinity]
lambda<0
, results [-infinity, 0]
Example: Pseudo-random float number using exponential distribution with lambda of 1.5: random.expovariate(1.5)
0.2234355
Discrete Event Simulation in Python