Using NumPy for simulations
Now let's learn how to use NumPy in a real-world scenario. Here, we will cover two examples of simulations using NumPy, and in the process, we will also learn about other operations that we can do with arrays.
Coin flips
We will look into a coin flip, or coin toss, simulation using NumPy. For this purpose, we will use the randint
function that comes in the random submodule from NumPy. This function takes the low
, high
, and size
arguments, which will be the range of random integers that we want for the output. So, in this case, we want the output to be either 0
or 1
, so the value for low
will be 0
and high
will be 2
but not including 2
. Here, the size
argument will define the number of random integers we want for the output, that is, the number of coins we will flip, in our case:

So we will assign 0
as tails and 1
as heads and the size
argument is assigned 1
, since we will be flipping one coin. We will get a different result every time we run this simulation.
Let...