# Lecture 30 – Randomness and Simulations¶

## Randomness¶

### np.random.randint and seeds¶

Each time you run this cell, you will get a random integer between 1 and 10 inclusive.

Each time you run this cell, you will get the number 9. Changing the seed (15) to something else may change the number you see. The seed is local to the cell only (no other cells in your notebook are affected by the np.random.seed(15) line).

The size argument allows us to get back an array instead of a single number.

Notice that the first five numbers in the following array are the same as in the array above.

Note that when I save an array to a variable, its result doesn't change unless I call np.random.randint again.

### np.random.choice¶

This is equivalent to flipping a fair coin once.

This is equivalent to flipping a fair coin 10 times.

## Simulations¶

### Example: dice rolls¶

Note: The term "empirical" means "from an experiment or simulation" (as opposed to theoretical).

Also note that the histogram is randomly generated each time you move the slider, so if you select 1000, move to some other number, and come back to 1000, the histogram will look different than it did before. (Setting the seed would prevent this from happening.)

### Example: sum of dice rolls¶

This array is the result of simulating the act of rolling two die and adding their results, 100 times.

Let's once again see what happens when we simulate this process a varying number of times.

## Repetition¶

### Simulating Coin Flips¶

Idea:

1. Flip a coin 100 times. Write down the number of heads.
2. Repeat step 1 many times – say, 10,000 times.
3. Draw a histogram of the number of heads in each iteration.

### The Birthday Problem¶

Instead of drawing a histogram, we're going to do something different. Run the cell below to generate a line plot.

It turns out that, in a class size of 23, the chances that two students share the same birthday is about 50%.

## Extra: Conway's Game of Life¶

You're not responsible for any of the code here. Just run the following cells. Much of this code is taken from here.

Also, this code will not work in the HTML output; you need to actually open the notebook for this to work.

### This is perhaps the only relevant line of code.¶

We randomly initialize a 20 by 20 grid of 1s and 0s.