Generate Random Fake People with Python

If you’re writing software, running quality assurance (QA) tests, or testing out a new data management tool, you might want to generate some random data. Although there are many other options out there, you can use Python for this purpose. There’s an open source Python package called Faker that will generate random data, including first names, last names, and other field types.

Installing Faker

Before proceeding, make sure you have Python 3 installed. You can use the pip3 command to install Python packages from PyPi. Install the Faker package using the below command.

sudo pip3 install faker --upgrade

Once you’ve installed the Faker package, fire up an instance of Python, by invoking the python3 binary.

python3

At the interactive Python prompt, type the following code to import and initialize the Faker generator. Of course, you can leave off the code comments, since they aren’t functionally doing anything.

# Import the Faker generator from the "faker" package
from faker import Faker

# Initialize a new Faker generator
faker = Faker()

Now that you’ve got Faker imported into your Python session, you can start using it to generate random data.

Using Faker to Generate Names

Faker is extensible using the notion of “providers.” You can read about providers that are included “out of the box” with the Faker package, over at the Faker documentation, under the Providers heading. 

If you drill into the person provider specifically, you’ll notice that there are a variety of different methods available on it. For example, you can generate a generic last name using the faker.last_name() method. Let’s give it a try now.

>>> faker.last_name()
'White'

You can also generate male or female first names using the faker.first_name_male() and faker.first_name_female() methods.

>>> faker.first_name_male()
'John'
>>> faker.first_name_female()
'Gina'

Next Steps

You can wrap this name generator functionality into your own functions to create data sets to help test out your software. For example, I will be using Faker to generate fake order records and ingest them into Amazon Kinesis data streams, so I can demonstrate the service’s functionality to my customers.

def create_person():
  return {
    'FirstName': faker.first_name(),
    'LastName': faker.last_name()    
  }

Check out the other providers available in the Faker package for Python. You can generate Python data structures, company names and tag-lines, credit card numbers and expiration dates, vehicle license plates, and much more. There’s also support for “community” providers, so you can build you own extension modules for Faker. Try it out!