LoginSignup
10
14

More than 5 years have passed since last update.

Pythonでランダムな文字列をつくる

Posted at
random.py
# 16進数(0-9, a-f)
def gen_rand_str_hex(length):
    import os
    import hashlib
    buf = ''
    while len(buf) < length:
        buf += hashlib.md5(os.urandom(100)).hexdigest()
    return buf[0:length]


# アルファベット大文字小文字+数字(0-9, a-z, A-F)
def gen_rand_str(length, chars=None):
    import random
    import string
    if chars is None:
        chars = string.digits + string.letters
    return ''.join([random.choice(chars) for i in range(length)])


10
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
14