Programming Challenge 0/99 Name Generator

To create a name generator first I need names list. Because there is no reason to create a new one, I can use an existing list from Github user dominictarr.

import random

def main():
    names_file = open("names.txt","r")
    firstnames_file = open("first-names.txt","r")
    names_buf = names_file.read()
    first_names_buf = firstnames_file.read()
    names = names_buf.split("\n")
    first_names = first_names_buf.split("\n")
    rand_name = names[random.randint(0,len(names))]
    rand_fname = first_names[random.randint(0,len(first_names))]
    print(rand_fname + " " + rand_name)


main()

As you see, the code is quite trivial. So, let me explain how it works. First, it read text files, then split variables by a newline(\n) and generate two variables by reading random values from existing arrays. Also, at the end of the function, it prints two variables with random first and last names.

Author of the names lists:

https://github.com/dominictarr/random-name

Leave a Comment