Pseudo-randomizing events and methods

Discrete Event Simulation in Python

Diogo Costa (PhD, MSc)

Adjunct Professor, University of Saskatchewan, Canada & CEO of ImpactBLUE-Scientific

Process (or event) randomization

What is randomization?

  • Process of making something random
  • In this case, making events (or processes) random
  • Useful to simulate non-deterministic processes

Worth noting: Computers incapable, by design, of generating truly random numbers.

  • Current time and other external variables often used in calculations to increase the randomness of the generator.
Discrete Event Simulation in Python

Random vs. pseudo-random

"Truly" random generation

  • Numbers truly random
  • Randomness limited by intrinsic machine limitations

Pseudo-random generation

  • Numbers generated based on probability distributions
  • Statistical distribution becomes apparent as samples increase

Example: Factory with manual assembly task

Scatter plot showing the duration of a particular manual assembly as it is repeated multiple times.

  • Probability distribution more appropriate to generate a pseudo-random next occurrence
Discrete Event Simulation in Python

Packages for pseudo-randomization

Several statistics packages can be used:

  • NumPy
  • SciPy
  • statsmodels
  • pandas
  • Random
  • scikit-learn

Random package

  • Generate random and pseudo-random numbers

Our focus

  • Methods commonly used in discrete-event models
Discrete Event Simulation in Python

Random number between given range

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
Discrete Event Simulation in Python

Random samples and sequences

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']
Discrete Event Simulation in Python

Gaussian and exponential distributions

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)
  • if lambda>0, results [0, infinity]
  • if 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

Let's practice!

Discrete Event Simulation in Python

Preparing Video For Download...