python 生成随机不重复的用户id

来源:互联网 发布:过山车大亨for mac 编辑:程序博客网 时间:2024/05/21 10:06

数据库里面有时候需要不重复的id 来表示用户id,就像QQ号码一样。

如果简单用uuid来生成的话,生成64位,太长。

生成6到8位gid

def generate_gid():    gids = []    for number in range(100000, 10000000):        gids.append(number)    for gid in gids:        index0 = random.randint(0, len(gids) - 1)        index1 = len(gids) - 1        tmp = gids[index0]        gids[index0] = gids[index1]        gids[index1] = tmp    return gids.pop()
速度还是可以的。

原创粉丝点击