$$ \newcommand{\tp}{\thinspace .} \newcommand{\Prob}[1]{\hbox{P}(#1)} $$

 

 

 

Random numbers and simple games

Hans Petter Langtangen [1, 2]

[1] Center for Biomedical Computing, Simula Research Laboratory
[2] Department of Informatics, University of Oslo

Jun 14, 2016


Table of contents

Drawing random numbers
      The seed
      Uniformly distributed random numbers
      Visualizing the distribution
      Vectorized drawing of random numbers
      Computing the mean and standard deviation
      The Gaussian or normal distribution
Drawing integers
      Random integer functions
      Example: Throwing a die
      Drawing a random element from a list
      Example: Drawing cards from a deck
      Example: Class implementation of a deck
Computing probabilities
      Principles of Monte Carlo simulation
      Example: Throwing dice
      Example: Drawing balls from a hat
      Random mutations of genes
      Example: Policies for limiting population growth
Simple games
      Guessing a number
      Rolling two dice
Monte Carlo integration
      Derivation of Monte Carlo integration
      Implementation of standard Monte Carlo integration
      Area computing by throwing random points
Random walk in one space dimension
      Basic implementation
      Visualization
      Random walk as a difference equation
      Computing statistics of the particle positions
      Vectorized implementation
Random walk in two space dimensions
      Basic implementation
      Vectorized implementation
Summary
      Chapter topics
      Example: Random growth
Exercises
      Exercise 1: Flip a coin times
      Exercise 2: Compute a probability
      Exercise 3: Choose random colors
      Exercise 4: Draw balls from a hat
      Exercise 5: Computing probabilities of rolling dice
      Exercise 6: Estimate the probability in a dice game
      Exercise 7: Compute the probability of hands of cards
      Exercise 8: Decide if a dice game is fair
      Exercise 9: Adjust a game to make it fair
      Exercise 10: Make a test function for Monte Carlo simulation
      Exercise 11: Generalize a game
      Exercise 12: Compare two playing strategies
      Exercise 13: Investigate strategies in a game
      Exercise 14: Investigate the winning chances of some games
      Exercise 15: Compute probabilities of throwing two dice
      Exercise 16: Vectorize flipping a coin
      Exercise 17: Vectorize a probablility computation
      Exercise 18: Throw dice and compute a small probability
      Exercise 19: Is democracy reliable as a decision maker?
      Exercise 20: Difference equation for random numbers
      Exercise 21: Make a class for drawing balls from a hat
      Exercise 22: Independent versus dependent random numbers
      Exercise 23: Compute the probability of flipping a coin
      Exercise 24: Simulate binomial experiments
      Exercise 25: Simulate a poker game
      Exercise 26: Estimate growth in a simulation model
      Exercise 27: Investigate guessing strategies
      Exercise 28: Vectorize a dice game
      Exercise 29: Compute \( \pi \) by a Monte Carlo method
      Exercise 30: Compute \( \pi \) by a Monte Carlo method
      Exercise 31: Compute \( \pi \) by a random sum
      Exercise 32: 1D random walk with drift
      Exercise 33: 1D random walk until a point is hit
      Exercise 34: Simulate making a fortune from gaming
      Exercise 35: Simulate pollen movements as a 2D random walk
      Exercise 36: Make classes for 2D random walk
      Exercise 37: 2D random walk with walls; scalar version
      Exercise 38: 2D random walk with walls; vectorized version
      Exercise 39: Simulate mixing of gas molecules
      Exercise 40: Simulate slow mixing of gas molecules
      Exercise 41: Guess beer brands
      Exercise 42: Simulate stock prices
      Exercise 43: Compute with option prices in finance
      Exercise 44: Differentiate noise measurements
      Exercise 45: Differentiate noisy signals
      Exercise 46: Model noise in a time signal
      Exercise 47: Speed up Markov chain mutation
References

Random numbers have many applications in science and computer programming, especially when there are significant uncertainties in a phenomenon of interest. The purpose of this document is to look at some practical problems involving random numbers and learn how to program with such numbers. We shall make several games and also look into how random numbers can be used in physics. You need to be familiar with basic programming concepts such as loops, lists, arrays, vectorization, curve plotting, and command-line arguments in order to study the present document. A few examples and exercises will require familiarity with the class concept in Python.

The key idea in computer simulations with random numbers is first to formulate an algorithmic description of the phenomenon we want to study. This description frequently maps directly onto a quite simple and short Python program, where we use random numbers to mimic the uncertain features of the phenomenon. The program needs to perform a large number of repeated calculations, and the final answers are "only" approximate, but the accuracy can usually be made good enough for practical purposes. Most programs related to the present document produce their results within a few seconds. In cases where the execution times become large, we can vectorize the code. Vectorized computations with random numbers is definitely the most demanding topic in this document, but is not mandatory for seeing the power of mathematical modeling via random numbers.

All files associated with the examples in this document are found in the folder src/random.