generating a random string python

1, generates a random string

1  # Digital Plus symbol alphabet + 
2  DEF getRandChar (n-):
 . 3      L = []  
 . 4      # Sample = '!. 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ @ # $% ^ & * () - + =' 
. 5      Sample = random.sample (string.ascii_letters string.digits +, 62 is ) generated from the ## a-zA-Z0-9 specified number of random characters: list type
 . 6      Sample Sample + = List ( ' - + = @ # $% ^ & * (!). ' ) # adding some elements based on the original symbols
 . 7      for I in Range (n-):
 . 8          char = the random.choice (sample) to select a character from the sample # in
 . 9          l.append (char)
 10      return  '' .join (L) # returns a string

2, a specified number of random characters generated

1  # generate the specified number of characters from a-zA-Z0-9 random character: 
2 ran_str = '' .join (random.sample (string.ascii_letters + string.digits,. 8))

3, a random character

1  # random character: 
2 Alphabet = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ @ # $% ^ & * ()! ' 
. 3 char = The random.choice (Alphabet)

4, generates a ipv4

1 def generateIpv4():
2     a = random.randint(0,255)
3     b = random.randint(0,255)
4     c = random.randint(0,255)
5     d = random.randint(0,255)
6     
7     ipv4 = '%d.%d.%d.%d'%(a,b,c,d)
8     return ipv4

 

Guess you like

Origin www.cnblogs.com/yaner2018/p/11269847.html