Random name generator in Python

In this short tutorial I will show you how to make a random name generator in Python.

If you came here looking for some cool random names visit the online name generators.

The name generator script

Open IDLE which is the Python supplied IDE or editor (Go to the bottom of the post to see, how you get started with Python).

Open a new window from the file menu and write the following name generator script into this window.

def name_generator():
  import random
  color = ["Red","Blue","Black","White"]
  tool = ["Hammer","Drill","Cutter","Knife"]
  randomNumber1 = random.randrange(0,len(color))
  randomNumber2 = random.randrange(0,len(tool))
  name = color[randomNumber1] + " " + tool[randomNumber2]
  print(name)

name_generator()

(Be aware that copy-paste from the blog may give syntax errors!
 A correct indent is an important element Python syntax!)

Save it as name-generator.py.  Now you can run the python program by pressing F5.

Name generator walk through

Lets walk through the python script, line by line.

  1. In the first line we define our name generator function.
  2. Then we import the random module, so we can call the random function later on.
  3. In line define 2 arrays (color and tool) and asign values to them
  4. Then we make 2 random numbers based on the length of our arrays. The randrage() function returns an integer in the range defined by the parameters. As parameters we use 0 and the len() function to count the numbers of words in the arrays.
  5. In line 7 and 8 we make the final name and print it on the screen.
  6. Finally we call the name generator function, we have just written.

That’s all it takes to make a simple Python random name generator. When you this basic functionality it’s easy to experiment and develop the random name generator to suit your needs and wishes.

Name generator inspiration

For inspiration visit the name generators where you can find a lot of name generators in several genres. From random names over fantasy and nicknames to band and business names.

Python Beginner guides and tutorials

Python is a free scripting language running on all platforms from Linux to Windows. Python is a relative easy programming language and a name generator is a simple application so it’s a good place to start if you wanna learn some programming.

If you haven’t Python installed at your computer you can download it at Python.org. This name generator script is based on python 3.12, which is the newest stable version at the moment.

At python.org you can find an excellent Python tutorial. And you can find much more about how to use the Random function in Python.

Or visit this beginner guide to Python which contains lots of links and resources.

This entry was posted in Name generator stuff and tagged , , , . Bookmark the permalink.

4 Responses to Random name generator in Python

  1. Python User says:

    I like Your Website very much.Because I am trying For these kind Of programmes from many days But I didn’t get the website which give the satisfactorial answer to me.But your’s website done that
    Please Give More excersises on RANDOM FUNCTION Usage.

    Thank you.

  2. Niels says:

    Hi Madhu. Great you find the python name generator useful.

    I’m planning on making a tutorial on more complex random scripts and random algorithms to use with my name generator scripts. Won’t be in python though, but in JavaScript. But the basic principals are the same. So stay tuned. :)

  3. Joshua Grigonis says:

    Why not

    return name

    then

    print(name_generator)

    This is a very good example for a beginner. I did a slightly more complicated name generator based on us census data, here:

    https://bitbucket.org/jgrigonis/thefranchise/src/42e881926aad/name_generator.py

  4. Niels says:

    Hi Joshua

    I’m not really a python guy, but more of a name generator dude. So things can definitely be done smarter.

    So thanks a lot for the input and the link. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>