Generate Random Data using Python

Published January 02, 2024

If you like to create random data for a dataset or are getting into artificial intelligence / machine learning or AI/ML, this article can be a starting point. At some point, you may want to generate your own dataset from random data, or just generate a lot of random data for learning.

Generate random data with Python

What is Randomness?

In this blog post, we will create random data. Randomness is found in everywhere, especially in machine learning. Cryptography is another area that uses randomness, in relation to ciphertext. Other areas using randomness are computer simulation, randomized design, slot machines and statistical sampling. A random process is a deterministic or indeterministic process for which what we do not know is greater than what we know. This delves into the Heisenberg's Uncertainty Principle, which is a fundamental concept in quantum physics.

Random Number Generators

Random number generation is a process by which a random number generator (RNG) a sequence of numbers or characters that cannot be predicted is generated. We will look into Python's built-in random module, which implements pseudo-random number generators (as opposed to truly random RNGs). Internally, the system time is used for randomness. This can be changed.

How to generate random numbers (integers)

To generate a random integer from a sequential list of numbers, we can use random.randint().

If we want to generate a random integer between and including 3 and 10, we call randint().

random.randint(3,10) translates to picking a random integer from this list of integers:

3, 4, 5, 6, 7, 8, 9, 10

import random

num = random.randint(3, 10)



Your output can vary, because it is.. well, random.

To generate a random number from a range of integers, we use random.randrange(). If we want to generate a random integer in this range

range(1, 100, 7), which evaluates to this array of integers:

[1, 8, 15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99]

This is how we call random.randrange():

import random

num = random.randrange(1, 100, 7)



It picked 64, which is in the whitelisted array of ints.

How to randomly pick a list of integers

Suppose we want to pick a list of 15 random numbers from 1 through 100. We can combine it with list comprehension.

import random

nums = [random.randint(1,100) for _ in range(15)]


[23, 62, 75, 56, 33, 29, 81, 55, 23, 45, 23, 6, 61, 86, 41]

How to randomly shuffle the list of integers

Let us randomly rearrange or shuffle the list of integers nums. For that, we will use random.shuffle().


The shuffle() method does not return anything. It shuffles all the items in nums in-place. So, when you print the value of nums, you get a new order of the existing items.


[64, 6, 23, 7, 31, 33, 58, 96, 59, 93, 21, 45, 94, 84, 3]

How to generate random floats

To generate a random float from a sequential list of numbers, we can use random.uniform(). This is very similar to random.randint().

If we want to generate a random float between and including 3 and 10, we call random.uniform(3,10):

import random

num = random.uniform(3, 10)



Your output can vary.

How to generate random floating point numbers

A floating point number is a number from 0.0 through 1.0. To generate a floating point number, we can use random.random().

Let us generate and print 10 floating point numbers:

for _ in range(10):



Shuffling a list of strings

Let us shuffle a list of strings. We will use the list fruits which contains ['apples', 'banana', 'oranges', 'pears']

fruits = ['apples', 'banana', 'oranges', 'pears']


['apples', 'banana', 'oranges', 'pears']
['pears', 'oranges', 'banana', 'apples']

How to generate random strings

To generate random strings from a list of characters, we will use random.sample(). This takes two parameters - the first is a string with all permissible characters, and the second is the length of the resultant list. We will combine all items in the list using join().

Let us create a 5-character password that consists of a combination of the following letters and numbers:

a, b, c, d, e, f, 4, 5, 6, 7, 8

password = ''.join(random.sample('abcdef45678', 5))



string module ascii_letters and digits

If you want to use all the letters of the alphabet, lowercase and uppercase, you can import string and use string.ascii_letters and string.digits.

string.ascii_letters evaluates to 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

string.digits evalutes to '0123456789'

What other functions are there in random?

To find a comprehensive list of functions in random, run this on the Python shell.


You will see a list of all functions available in the random module, along with description of each.

Can I use the random module for security or cryptographic purposes?

No. The pseudo-random generators of the random module are meant for modeling and simulation. For generating cryptographically strong and random numbers to be used as passwords, account authentication, security tokens, etc, we should use the secret module.

You can read more about the secret module here.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: January 02, 2024. ย ย ย  This post was originally written on January 02, 2022.