Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- from itertools import takewhile
- def accumulate(iterator):
- """Returns a cumulative sum of the elements.
- accumulate([1, 2, 3, 4, 5]) --> 1 3 6 10 15"""
- current = 0
- for value in iterator:
- current += value
- yield current
- def weightedChoice(weights, objects):
- """Return a random item from objects, with the weighting defined by weights
- (which must sum to 1)."""
- limit = random.random()
- return objects[sum(takewhile(bool, (value < limit for value in accumulate(weights))))]
- letters = ["a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","u","U","v","V","w","W","x","X","y","Y","z","Z"]
- irl = []
- ric = []
- irlchances = []
- words = []
- rwc = []
- wordchances = []
- with open("realnames.txt") as f:
- for line in f:
- irl.append(line.rstrip().lower())
- with open("creal.txt") as f:
- for line in f:
- ric.append(float(line.rstrip()))
- for val in ric:
- irlchances.append(val * (1/sum(ric)))
- with open("words.txt") as f:
- for line in f:
- words.append(line.rstrip())
- with open("cwords.txt") as f:
- for line in f:
- rwc.append(math.sqrt(float(line.rstrip())))
- for val in rwc:
- wordchances.append(val * (1/sum(rwc)))
- def gen(max_length=16):
- initial = random.randint(1,2)
- if initial == 1:
- bases = irl
- chances = irlchances
- elif initial == 2:
- bases = words
- chances = wordchances
- base = weightedChoice(chances,bases)
- base = base[0:1].upper() + base[1:len(base)].lower()
- result = base
- #print("Base is " + base)
- result = base * (max_length + 1)
- tries = 0
- while len(result) > max_length:
- tries += 1
- if tries > 32:
- return "FAILED"
- result = base
- mods = []
- if random.randint(1,2) == 1:
- mods.append("l")
- if random.randint(1,2) == 1:
- mods.append("w")
- if random.randint(1,2) == 1:
- mods.append("i")
- if random.randint(1,2) == 1:
- mods.append("n")
- random.shuffle(mods)
- #print("--Mods--")
- for mod in mods:
- ladd = ""
- if mod == "l":
- i = 0
- max = random.randint(1,3)
- while i < max:
- i += 1
- ladd += random.choice(letters)
- result += ladd
- #print("Added letters \"" + ladd + "\"")
- elif mod == "w":
- i = 0
- max = random.randint(1,2)
- while i < max:
- i += 1
- caps = random.randint(1,2)
- randword = weightedChoice(wordchances,words)
- if caps == 2:
- randword = randword[0:1].upper() + randword[1:len(randword)].lower()
- ladd += randword
- result += ladd
- #print("Added words \"" + ladd + "\"")
- elif mod == "i":
- randname = weightedChoice(irlchances,irl)
- ladd += randname[0:1].upper() + randname[1:len(randname)].lower()
- result += ladd
- #print("Added name \"" + ladd + "\"")
- elif mod == "n":
- i = 0
- max = random.randint(1,5)
- while i < max:
- i += 1
- num = random.randint(0,9)
- ladd += str(num)
- result += ladd
- #print("Added number \"" + ladd + "\"")
- #print(str(len(result)) + ": " + result)
- #print("--Mods--")
- return result
Advertisement
Add Comment
Please, Sign In to add comment