# Creating a string of random characters

I'm generating domains with random names. I used the map function because it packages the results into a list which I can then join() into a string but the way I used the lambda function could be confusing to people reading this later.

def generateHost():
charSet = 'abcdefghijklmnopqrstuvwxyz1234567890'
minLength = 5
maxLength = 30
length = random.randint(minLength, maxLength)

return ''.join(map(lambda unused : random.choice(charSet), range(length)))+".com"


This function calls generateHost() 'size' times and creates an array of hosts. I could write it like the above but it would be the same problem. I wrote it with a for loop which I'm still not satisfied with.

def generateHostList(size):
result = []
for i in range(size):
result += [generateHost()]
return result

-

The answer to this question suggests the following:

''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length))


It's close to your solution, but avoids having to type out the whole alphabet by using predefined values and avoids the use of map() and lambda.

-
Wow. Set builder notation is amazing. Any idea why 'random.choice(string.ascii_uppercase + string.digits) for _ in range(N)' doesn't have to be in brackets? –  James Fargotson Apr 18 '14 at 4:53
Looks like you missed [] –  vnp Apr 18 '14 at 4:53
It works without the brackets when it's within joins parenthesis. I've been trying to figure out why. –  James Fargotson Apr 18 '14 at 5:08
@JamesFargotson The reason it works is that it is a generator expression, not a set comprehension, so the parentheses can be omitted on calls with only one argument. –  codesparkle Apr 18 '14 at 6:53
@codesparkle Very cool. Thanks for the links. –  James Fargotson Apr 19 '14 at 17:34

A pythonic idiom is to utilize a list comprehension where possible:

def generateHostList(size):
return [ generateHost() for _ in range(size) ]


(notice another idiom of using _ for unused).

The generateHost itself may look confusing. The TomBarron's solution is better.

-

The other posts already answered how to generate random strings and a random list of hosts and are totally spot on.

I would just add that if this is for generating hostnames in a network, they might not be very great names. They look more like passwords, which are difficult to remember and won't be practical. I recommend a different approach, as suggested by this article. The idea is to pick a themes, and use a mix of them as server names. For example, you could use a mix of cartoon character names, like:

toystory = ('woody', 'buzz', 'jessie', 'rex', 'potato', 'sid')
walle = ('walle', 'eve', 'axiom', 'captain')
'-'.join([random.choice(toystory), random.choice(walle)])


Of course, ideally with much longer lists. And you can still throw in a number part at the end, for example:

'-'.join([random.choice(toystory), random.choice(walle), ''.join(random.choice(string.digits) for _ in xrange(3))])


Finally, take a look at PEP8, the style guide of Python. So far your code is ok, but it's recommended to indent with 4 spaces instead of 2. There is a command line tool called pep8 which can check your code for style. As you're learning Python, it's good to follow the recommended style right from the start, easier than correcting bad habits later.

-
Thanks for letting me know about the style guide. The second entry is an Emerson quote so I think I'm going to like it.The domain names wont be managed by humans so remembering them won't be a problem. –  James Fargotson Apr 18 '14 at 6:04