[ACCEPTED]-Simple random name generator in Python-python
Serialize (pickle) a dictionary to a file instead.
Example:
# create the dict and save it to a file
d={
'part1':[
'Ae',
'Di',
'Mo',
'Fam',],
'part2':[
'dar',
'kil',
'glar',
'tres',],
}
import pickle
f=open('syllables','w')
pickle.dump(d,f)
f.close()
# read the dict back in from the file
f1=open('syllables','r')
sd=pickle.load(f1)
f1.close()
import random
first_part=sd['part1'][random.randint(0,len(sd['part1'])-1)]
second_part=sd['part2'][random.randint(0,len(sd['part2'])-1)]
print '%s%s'%(first_part,second_part)
0
import random
parts = {}
with open('parts.txt', 'r') as f:
currentList = []
for line in f.readlines():
line = line.strip()
if line.startswith('[') and line.endswith(']'):
currentList = []
parts[line[1:-1]] = currentList
else:
currentList.append(line.strip())
for i in xrange(10):
print ''.join(random.choice(parts[partName]) for partName in sorted(parts))
returns (randomly):
Aekil
Didar
Mokil
Mokil
Moglar
Moglar
Diglar
Famdar
Famdar
Modar
0
You'll have to read through the entire file 17 at some point, unless you know beforehand 16 how many prefixes and suffixes there are. Since 15 I assume you don't, or that it can change 14 and you don't want to maintain a number 13 for storing that, you'll have to read through 12 the file, and readline() is a good way of 11 doing that.
However, you can preprocess your 10 text file so that it uses another format, such 9 as a pickle file. In other words, read the text 8 file into a dictionary, and pickle that 7 dictionary. The dictionary might look something 6 like this:
dic = {'prefixes': ['Ae' ,'di', ...],
'suffixes': ['dar', 'kil', ...]}
From the lenght of the arrays 5 you can then determine what the maximum 4 random number is. It should be more efficient 3 than reading an entire file line-for-line 2 each time. And if not, at least it's a bit 1 more elegant solution.
Modified @eumiro's script:
#!/usr/bin/env python
import fileinput
import random
import re
from collections import defaultdict
partname = ''
parts = defaultdict(list)
for line in fileinput.input():
line = line.rstrip()
if line.startswith('[') and re.match(r'\[part\d+\]', line):
partname = line
else:
parts[partname].append(line)
parts_list = list(map(parts.get, sorted(parts)))
for _ in range(10):
print(''.join(map(random.choice, parts_list)))
Output
Famglar
Famkil
Didar
Ditres
Aedar
Famglar
Ditres
Famtres
Ditres
Modar
0
I once found a unique, pseudo name generator 5 I used quite often at a website called domainhole, but 4 then security warnings and alerts popped 3 every time I tried to visit the site. So 2 I wrote a script to do the same thing it 1 was doing.
import random
vowels = [97,101,105,111,117]
consonants = [98,99,100,102,103,104,106,107,108,109,110,112,113,114,115,116,118,119,120,121,122]
def get_character(seed):
match seed:
case 'L':
return chr(random.randint(97,122))
case 'C':
return chr(consonants[random.randint(0,len(consonants)-1)])
case 'V':
return chr(vowels[random.randint(0,len(vowels)-1)])
case _:
return -1
# enter custom values here: C for consonants, V for vowels, and L for all letters
pseudonym_array = ['C','L','V','L']
pseudonym = ''
for i in range(len(pseudonym_array)):
pseudonym += get_character(pseudonym_array[i])
print(pseudonym)
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.