Python Random Data Generation

python random

In this post, we will discuss how to generate random data(Numbers and Strings) in Python.

Random numbers can be generated using the Python standard library or using Numpy.

How to generate random numbers using the Python standard library?

The Python standard library provides a module called random, which contains a set of functions for generating random numbers.

The Python random module uses a popular and robust pseudo random data generator.

The python random data generator is  called the Mersenne Twister.

Let us now look at the process in detail.

Using seed()

Firstly, we need to understand why we need to call the seed() function for generating random number.

Let us try to generate random number without calling seed() function. Then we will know the impact of using seed().

The random() function without calling seed() returns a random float in the interval [0.0, 1.0). Each subsequent call to random() will generate a new float value!!

If we call seed() function before calling random(), the chain of calls after random.seed() will produce the same trail of data:

The example below demonstrates seeding the pseudorandom number generator, generates some random numbers, and shows that reseeding the generator will result in the same sequence of numbers being generated.

how to generate random numbers in python

Running the example seeds the pseudorandom number generator with the value 4, generates 3 random numbers, reseeds the generator, and shows that the same three random numbers are generated.

Notice the repetition of “random” numbers. The sequence of random numbers becomes deterministic, or completely determined by the seed value, 4

It can be useful to control the random output by setting the seed to some value to ensure that your code produces the same result each time.

How to generate random floating point values in Python?

Using random()

By calling seed() and random() functions from Python random module, you can generate random floating point values as well.

Values will be generated in the range between 0 and 1.

The example below generates 10 random floating point values.

how to generate random floating numbers in python

Using uniform()

If you need to generate random floats that lie within a specific [x, y] interval, you can use random.uniform()

generate python random floating numbers using uniform

How to generate random integer values between x and y?

Using randint()

Using Python random module, integer values can be generated with the randint() function.

This function takes two arguments: the start and the end of the range for the generated integer values.

Random integers are generated within and including the start and end of range values, specifically in the interval [start, end].

The example below generates 10 random integer values between 0 and 10.

how to generate random integers in python

Using randrange()

With random.randrange(), you can exclude the right-hand side of the interval, meaning the generated number always lies within [x, y) and will always be smaller than the right endpoint.

python random randrange example

How to generate random gaussian values in Python?

Using gauss()

Random floating point values , which are part of a Gaussian distribution, can be generated using the gauss() function.

This function takes two arguments that correspond to the parameters that control the size of the distribution, namely wthe mean and the standard deviation.

If you want to generate 10 random values from a Gaussian distribution with a mean of 0 and a standard deviation of 1, then use random.gauss(0,1)

how to generate random gaussian values in python

How to randomly select individual values from a list?

Using choice()

As you might be knowing already, to select a particular value from a list, we need to use an index.

So in order to randomly select values from a list, we should randomly generate the corresponding  index.

Python random module has a standard built in function called choice. Using this function, we can randomly select values from any list or any Python sequence.

selection of elements using python random choice

Using choices()

You can also use random.choices() for choosing multiple elements from a list.

python random selection of multiple elements from a list

How to randomly generate subset of a list in Python?

We may be interested to create a randomly chosen subset from a list.

This means, once an value is selected from the list and added to the subset, it should not be added to the subset again. This is called selection without replacement.

Using sample()

This behavior can be achieved using the sample() function in the Python random module.

The sample() function takes a list and the size of the subset as arguments.

Note that elements are not actually removed from the original list, only selected into a copy of the list.

The example below demonstrates selecting a subset of 2 items from a list of 5 integers.

generate random subset of list in python

How to randomly shuffle a list in Python?


The shuffle() function in Python random module can be used to shuffle a list.

The shuffle is performed in place, meaning that the list provided as an argument to the shuffle() function is shuffled rather than a shuffled copy of the list being made and returned.

The example below demonstrates randomly shuffling a list of integer values.

random shuffle of a list in python

How to generate random string in Python?

Approach 1

Using a combination of random and string modules, we can generate random strings.In Approach 1, we will see how to use random.choice() and random.choices() functions.

Note that string module contains sequences of common ASCII characters.

generating python random strings

The below code is a more secure version cryptographically:

how to generate python secure random string

Approach 2

Using uuid module.

python random string using uuid

How to generate random ndarray using Numpy?

NumPy also has its own implementation of a pseudorandom number generator and utility wrapper functions.

Note that the NumPy pseudorandom number generator is different from the Python standard library pseudorandom number generator and hence, seeding the Python pseudorandom number generator does not impact the NumPy pseudorandom number generator.

It must be seeded and used separately.

Using seed()

The example below demonstrates how to seed the generator using Numpy, and how reseeding the generator will result in the same ndarray(n dimensional array) being generated.

generating random number using numpy

How to generate random ndarray containing float values?

An array of random floating point values can be generated with the rand() NumPy function.

Using rand()

If no argument is provided while calling rand(), then a single random value is created, otherwise the size of the n-dimensional array can be specified.

In the below example, I am generating a 3 x 2 ndarray.

generating random ndarray using numpy rand

Refer to example below for creating an array of 5 random floating point values.

What is the difference between np.random.rand() vs np.random.random()?

numpy.random.random() is an alias for numpy.random.random_sample()

The only difference is in how the arguments are handled.

With numpy.random.rand, the shapes of the output array are separate arguments.

With numpy.random.random_sample, the shape argument is a single tuple.

For example, to create an array of samples with shape (3, 5), you can write


What is the difference between various numpy random functions?


They’re just aliases to random_sample.

how to generate random integer values using Numpy

An array of random integers can be generated using the randint() NumPy function.

Using randint()

randint() takes 4 parameters – low, high, size and dtype.

low – the lower end of the range.

high – the upper end of the range.An optional parameter.

size – the number of integer values to generate.

dtype – desired data type of the result. An optional parameter.

Refer to below example where only low parameter is provided.So random integers between 0 to 19 will be generated.

Consider another example where both low and high parameters are provided.So the integers between 20 to 29 will be considered.

In order to generate a 2 x 4 array of ints between 0 and 4, inclusive:

How to shuffle numpy array?

Using shuffle()

A NumPy array can be randomly shuffled in-place using the shuffle() NumPy function.The example below demonstrates how to shuffle a NumPy array.

That’s it for now on the random generation of data in Python.

Any questions, feel free to ask in the comment section below.

You must be logged in to post a comment.
Improve Your Data Science Skills Today!

Subscribe To Get Your Free Python For Data Science Hand Book


Arm yourself with the most practical data science knowledge available today.