Creating a random string

Hey guys.

In the rigify add-on, when you generate a rig, it gives the rig a random string as it’s ID. How is this generated? I have been trying to do some things and they would need something similar to this. I have been tearing apart the rigify scripts, but I can’t find it anywhere.

Thanks,
Ickathu

It is the random_id function at the end of utils.py


import bpy,random,time

def random_id(length = 8):
    """ Generates a random alphanumeric id string.
    """
    tlength = int(length / 2)
    rlength = int(length / 2) + int(length % 2)

    chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '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']
    text = ""
    for i in range(0, rlength):
        text += random.choice(chars)
    text += str(hex(int(time.time())))[2:][-tlength:].rjust(tlength, '0')[::-1]
    return text

#### this will print a 12 chars random string
print (random_id(12))



Thank you so much!!

Too late… but may as well!


import random, string

#charset=string.printable
charset=string.ascii_letters+string.digits

def rand_str(length=8):
    max=len(charset)
    s=""
    for i in range(length):
        s+=str(charset[random.randint(0,max)])
    return s

print(rand_str())

Thanks, rarebit!!
For some reason, the part from the end of utils.py is not working properly for me (it has some charactars that I did not specify), but yours works perfect! thanks both of you!

anyone knows if there is command to identify any UN8 special characters in a string ?

thanks