Trey Hunner

programming, teaching, open source

Random Name Generator in Python

| Comments

I’ve used multiple websites to generate random names for my test data when running manual or automated QA tests.

Since discovering DuckDuckGo’s random word generation, I’ve hoped that someone would make a DuckDuckGo plugin to generate random names also.

I didn’t want to write my own DuckDuckGo plugin yet so I made a Python-powered command line tool instead using 1990 Census data.

The program is called names and can be found on Github and PyPI.

It’s really simple

It’s basically just one file currently that’s about 40 lines long. There is only one feature available from the command line currently: generate a single random full name. There’s a few more features if importing as a Python package: generate random last name or generate random first name (with or without specifying gender), generate random full name (also without or without gender).

The random name picker relies on the cumulative frequencies listed in the included Census data files. Here’s the steps that are taken: 1. A random floating point number is chosen between 0.0 and 90.0 2. Name file lines are iterated through until a cumulative frequency is found that is less than the randomly generated number 3. The name on that line is chosen and returned (or printed out)

Examples

Here’s how you use it from the command line:

$ names
Kara Lopes

Here’s how you use it as a Python package:

>>> import names
>>> names.get_full_name()
u'Patricia Halford'
>>> names.get_full_name(gender='male')
u'Patrick Keating'
>>> names.get_first_name()
'Bernard'
>>> names.get_first_name(gender='female')
'Christina'
>>> names.get_last_name()
'Szczepanek'

Learn more through weekly Python chats 🎁

Like my teaching style? Want to learn more? Sign up for attend my Weekly Python Chat events so I can answer your questions about Python, programming, and life in general.