python获取 需要的 随机数格式

要产生自己需要的随机数格式。python2.7.

# 产生随机数,状态有三种,num为数字,let为字母,mix为混合
def create_random(rand_type, rand_len):
    import random
    list_num = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
    list_let = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "g", \
                "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", \
                "u", "v", "w", "x", "y", "z"]
    rand_str = ""
    if rand_type == "num":
        for ll in xrange(rand_len):
            rand_str = "%s%s" % (rand_str, random.sample(list_num, 1)[0])
    elif rand_type == "let":
        for ll in xrange(rand_len):
            rand_str = "%s%s" % (rand_str, random.sample(list_let, 1)[0])
    elif rand_type == "mix":
        list_all = list_num + list_let
        for ll in xrange(rand_len):
            rand_str = "%s%s" % (rand_str, random.sample(list_all, 1)[0])
    else:
        rand_str =""
    return rand_str

发表评论

电子邮件地址不会被公开。 必填项已用*标注