2 Readability edit edited Jan 6 '14 at 13:03 Joachim Isaksson 1,16155 silver badges99 bronze badges Python 2.7 (149) from random import* ''.join(map(lambda x:chr(randint(*(x or xor(32,126)))),sorted(map(None,sample(range(15),15),((48,57),(65,90),(97,122),(33,47))))))  Written out in a more readable (and not executable) way; from random import * ''.join( # Concatenate characters to string map( # Map all characters using below lambda lambda x:chr(randint(*(x or (32, 126)))), # Map a single range to a random character # within a specific range if supplied, # otherwise the default "all" range. sorted( # After distributing ranges, sort map(None, # zip_longest alternative, distributes the # required ranges over 4 random positions sample(range(15), 15), # 0-14 in random order ((48, 57), (65, 90), (97, 122), (33, 47)) # The 4 required ranges ) ) ) )  Fairly straight forward and surprisingly not much longer than a "generate, retry on match fail" version. Python 2.7 (149) from random import* ''.join(map(lambda x:chr(randint(*(x or (32,126)))),sorted(map(None,sample(range(15),15),((48,57),(65,90),(97,122),(33,47))))))  Written out in a more readable (and not executable) way; from random import * ''.join( # Concatenate characters to string map( # Map all characters using below lambda lambda x:chr(randint(*(x or (32,126)))), # Map a single range to a random character # within a specific range if supplied, # otherwise the default "all" range. sorted( # After distributing ranges, sort map(None, # zip_longest alternative, distributes the # required ranges over 4 random positions sample(range(15),15), # 0-14 in random order ((48,57),(65,90),(97,122),(33,47)) # The 4 required ranges ) ) ) )  Fairly straight forward and surprisingly not much longer than a "generate, retry on match fail" version. Python 2.7 (149) from random import* ''.join(map(lambda x:chr(randint(*(xor(32,126)))),sorted(map(None,sample(range(15),15),((48,57),(65,90),(97,122),(33,47))))))  Written out in a more readable (and not executable) way; from random import * ''.join( # Concatenate characters to string map( # Map all characters using below lambda lambda x:chr(randint(*(x or (32, 126)))), # Map a single range to a random character # within a specific range if supplied, # otherwise the default "all" range. sorted( # After distributing ranges, sort map(None, # zip_longest alternative, distributes the # required ranges over 4 random positions sample(range(15), 15), # 0-14 in random order ((48, 57), (65, 90), (97, 122), (33, 47)) # The 4 required ranges ) ) ) )  Fairly straight forward and surprisingly not much longer than a "generate, retry on match fail" version. 1 answered Jan 6 '14 at 12:53 Joachim Isaksson 1,16155 silver badges99 bronze badges Python 2.7 (149) from random import* ''.join(map(lambda x:chr(randint(*(x or (32,126)))),sorted(map(None,sample(range(15),15),((48,57),(65,90),(97,122),(33,47))))))  Written out in a more readable (and not executable) way; from random import * ''.join( # Concatenate characters to string map( # Map all characters using below lambda lambda x:chr(randint(*(x or (32,126)))), # Map a single range to a random character # within a specific range if supplied, # otherwise the default "all" range. sorted( # After distributing ranges, sort map(None, # zip_longest alternative, distributes the # required ranges over 4 random positions sample(range(15),15), # 0-14 in random order ((48,57),(65,90),(97,122),(33,47)) # The 4 required ranges ) ) ) )  Fairly straight forward and surprisingly not much longer than a "generate, retry on match fail" version.