Advertisement
Debug__

Random String Generator (Python)

Dec 22nd, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import random
  2.  
  3. def randomString(length):
  4.     lower = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'.split(',')
  5.     caps = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z'.split(',')
  6.     numbers = '0,1,2,3,4,5,6,7,8,9'.split(',')
  7.     special =  '!,@,#,$,%,^,&,*,(,),-,_,~,=,+'.split(',')
  8.     try:
  9.         float(length)
  10.     except ValueError:
  11.         length = 32
  12.     x = array_merge(array_merge(numbers, special), array_merge(lower, caps))
  13.     random.shuffle(x)
  14.     str = ''
  15.     for ind in range(0, length):
  16.         str += x[ random.choice(range(len(x))) ]
  17.     return str
  18.    
  19. def array_merge(first_array, second_array):
  20.     if isinstance(first_array, list) and isinstance(second_array, list):
  21.         return first_array + second_array
  22.    
  23.     elif isinstance(first_array, dict) and isinstance(second_array, dict):
  24.         return dict(list(first_array.items()) + list(second_array.items()))
  25.    
  26.     elif isinstance(first_array , set) and isinstance(second_array , set):
  27.         return first_array.union(second_array)
  28.    
  29.     return False
  30.    
  31. length = 30
  32. String = randomString(length)
  33. print(String)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement