📜  在Python中生成随机 ID

📅  最后修改于: 2022-05-13 01:54:59.300000             🧑  作者: Mango

在Python中生成随机 ID

在Python中有不同的方法来生成 id。让我们看看如何在不使用内置Python库的情况下使用Python生成不同类型的 Id。

1.生成随机整数作为ID'

代码 #1:打印 10 个 1 到 100 之间的随机数字值。

Python3
# Python3 code to demonstrate the
# random generation of Integer id's
 
import random
 
# determines how many values
# will be printed
for x in range(10):
     
    # print 10 random values
    # between 1 and 100
    print (random.randint(1, 101))


Python3
# Python3 code to demonstrate
# the random generation of id's
# which are multiple of 5
 
import random
 
# determines how many
# values will be printed
for x in range(10):
         
    # print 10 random values between
    # 1 and 100 which are multiple of 5
    print (random.randint(1, 20) * 5)


Python3
# Python3 code to demonstrate the
# random generation of string id's
 
import random
import string
 
# Generate a random string
# with 32 characters.
random = ''.join([random.choice(string.ascii_letters
            + string.digits) for n in range(32)])
 
# print the random
# string of length 32
print (random)


Python3
# Python3 code to demonstrate
# the random generation of string id's
 
import random
import string
 
# defining function for random
# string id with parameter
def ran_gen(size, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for x in range(size))
 
# function call for random string
# generation with size 8 and string
print (ran_gen(8, "AEIOSUMA23"))


输出 :

76
72
7
78
77
19
24
23
77
96

代码 #2:打印 1 到 100 之间的随机数,它们是 5 的倍数。

Python3

# Python3 code to demonstrate
# the random generation of id's
# which are multiple of 5
 
import random
 
# determines how many
# values will be printed
for x in range(10):
         
    # print 10 random values between
    # 1 and 100 which are multiple of 5
    print (random.randint(1, 20) * 5)

输出 :

60
30
35
100
85
25
100
20
90
85

2.生成随机字符串作为ID'

生成随机字符串id 由字母和数字组成。这在生成密码时很有用,因为它提供了加密和解密技术。

代码 #1:展示如何生成随机字符串id。

Python3

# Python3 code to demonstrate the
# random generation of string id's
 
import random
import string
 
# Generate a random string
# with 32 characters.
random = ''.join([random.choice(string.ascii_letters
            + string.digits) for n in range(32)])
 
# print the random
# string of length 32
print (random)

输出 :

Rf2IdqUNkURNN6mw82kSpyxQe9ib3usX

代码 #2:使用函数调用

Python3

# Python3 code to demonstrate
# the random generation of string id's
 
import random
import string
 
# defining function for random
# string id with parameter
def ran_gen(size, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for x in range(size))
 
# function call for random string
# generation with size 8 and string
print (ran_gen(8, "AEIOSUMA23"))

输出 :

S2M2IEAO

在Python中使用 UUID 生成随机 id