我们可以使用Python内置的随机数生成函数以及UUID模块来生成虚拟唯一记录。其中,UUID模块可以生成唯一的UUID字符串,确保每个记录都是唯一的。
代码示例:
import random import uuid
def generate_unique_records(n): """ 生成最多N条虚拟唯一记录 """ unique_records = []
while len(unique_records) < n:
# 生成随机数
rand_num = random.randint(1, n*10)
# 生成唯一的UUID字符串
uuid_str = str(uuid.uuid4())
# 拼接记录
record = f"{rand_num}_{uuid_str}"
# 判断是否唯一,并加入列表
if record not in unique_records:
unique_records.append(record)
return unique_records
unique_records = generate_unique_records(10) print(unique_records) #输出生成的唯一记录列表