Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Randomness for Modeling and Simulation

Give Feedback

Welcome to video 2 in Generating Random Data in Python. In the last video, you heard that the random module provides pseudo-randomness.

That means the random data generated from the methods in random are not truly random. The random module is an example of a PRNG, the P being for Pseudo. A True random number generator would be a TRNG and typically involves hardware. In the real world, rolling an unbiased die is an example of a TRNG.

What makes the random module a PRNG? First, it’s implemented in software, and by design can be seeded to be deterministic. In other words, we can recreate and predict the generated series of random values. Data generated from random are produced based on a value we call the seed. You can think of the seed as a starting point to get the random generation going.

When you invoked the random methods you learned in the last video, the random module had to come up with its own seed, typically your system time. It then uses that seed in an algorithm to generate values. The random module also has a method called random(). Let’s see it in action.

random.random() generates a float value equal to or greater than 0.0 but less than 1.0, which is conveyed with the notation [0.0, 1.0) to indicate that the first value is inclusive, but the second value is exclusive.

While it’s convenient that the random module can seed off of system time, sometimes you’ll want to repeat a random sequence for testing or demonstration.

For this purpose, there is the seed() method. Pass an int argument, and the method will use it as the seed. As a side note, you may also pass seed() a string, bytes, or byte array, and then those values will be converted to an integer before use.

In this example, you’ll see the effect of explicitly seeding random(). It provides us a way to duplicate the same random generation, which is a handy tool for testing.

In addition to seeding, we can capture the state of random() at any time with the getstate() method. This returns a tuple that we can then pass to a companion setstate() method to duplicate the generation at that moment.

Data Science: The numpy.random Module#

Because simulation is such a common implementation of pseudo-random generation, it’s important to talk about its application in data science, and its use in the NumPy package.

This video will cover a few of these functions in NumPy, but NumPy could be a course all on its own. There are many tutorials covering NumPy in depth available on Real Python.

NumPy contains its own random module. Where the standard random module provided us a convenient way of generating random scalar values, NumPy’s random implementation is more geared towards random series of data. Let’s go ahead and import it and get to work.

Here we’re using a Jupyter notebook to demonstrate some basic NumPy random methods. We first import NumPy with the alias np. See how NumPy’s random duplicates many of the same methods and method names as the standard random module? These include random(), randint(), seed(), and others.

These methods mostly function the same.

Both the random() and seed() work similarly to the one in the standard random.

It appears randint() also works in a similar way, but there are a couple differences that I’ll explain later.

Here, you see that we can re-run our random seed cell to reset our randint() results.

For sequences, we also have a similar choice() method.

But in NumPy, there is no choices() method. The sample() in NumPy’s random is very different. If you pass a sequence argument, then it’s read as the size for a multi-dimensional array.

In this next code, we’re running randint() to simulate the roll of a die. This is to illustrate some differences from the standard randint():

  1. The upper bounds is exclusive, requiring us to have 6+1 as our upper bounds in order for the 6 to be included in the possibilities.
  2. We can pass a third argument to get an array with that number of elements, in this case 100 rolls.

Repeatedly rolling a die would result in a uniform distribution of values between 1 and 6, and there is an np.random.uniform() method we could use with the same arguments, but it produces floats.

When it comes to rolling two dice, that will look more like a normal distribution or bell curve. We can see this is true if we create a second die roll and combine them with the first die roll. When added together, the most likely result would be 7, and the least likely results would 2 and 12.

We can see the result graphically with Matplotlib, but it’s better illustrated if we increase our data samples to 5000.

That brings us to the normal() method, but like uniform(), it produces floats. It will give us values that would resemble a bell curve however. In the standard random, we do have a normalvariate() method. It requires mean and standard deviation arguments, and it returns only one value.

random gives us a normal distribution in the shape we specify in the arguments.

Now for just one more illustration. We know some factors grow or decrease relative to other factors. This is known as correlation. NumPy can build correlated random data given a mathematical covariance. This function here will get that for us.

Let’s suppose we have a correlation matrix with 1, 0.9, and 0.9, 1. This means we have a strong correlation.

Let’s suppose we’re talking about age as one data set, and percentage of gray hair as the second data set. As age grows, so does the chance that percentage will increase. My numbers might be off from real life, but bear with me.

You can see that our ages and percentages are floats, and some of our gray hair percentages are negative, but that’s more because I couldn’t think of a good example. You see, however, that the older people in this cross section of data do have higher percentages of gray hair.

If we scatter plot these points, we see the diagonal trend that suggests our correlation between age and gray hair.

Comparing random vs numpy.random#

Let’s wrap this up by comparing some of the features in the standard random side by side with the corresponding features in NumPy random.

Finally, remember that if you only need a single random value or a small sequence, then standard random is usually the faster and better option. NumPy is specialized for building large, multi-dimensional arrays.

You’ve now seen the benefits of pseudo-randomness along with situations where you might want to repeat your random data generation. This feature makes the PRNGs like the random module great for simulation, but not so great for security. In the next video, you’ll know why. See you there!

Chaitanya on June 29, 2019

comparision between standard random and numpy random is not explained in a detailed way, the correlation example is also not clear

Pygator on Sept. 2, 2019

What is that run cell button on the side? Don’t think that’s with my notebooks. Is that an extension feature?

Become a Member to join the conversation.