Random string generation with upper case letters and digits

前端 未结 30 3540
逝去的感伤
逝去的感伤 2020-11-22 02:51

I want to generate a string of size N.

It should be made up of numbers and uppercase English letters such as:

  • 6U1S75
  • 4Z4UKK
  • U911K4
30条回答
  •  野的像风
    2020-11-22 03:10

    Use Numpy's random.choice() function

    import numpy as np
    import string        
    
    if __name__ == '__main__':
        length = 16
        a = np.random.choice(list(string.ascii_uppercase + string.digits), length)                
        print(''.join(a))
    

    Documentation is here http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.random.choice.html

提交回复
热议问题