random是python中用来生存随机数的库。具体用法如下:
random.random()
random.uniform(1,2)
random.randint(a,b)
random.choice()
random.sample(序列, k)
random.shuffle()
random.seed(x)
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
print(string.punctuation)
import random
def create_phone(num):
# 用于存放生成的数据
t_set = set()
# 用于存放号段
yidonglist = [134, 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 178, 182, 183, 184, 187]
liantonglist = [130, 131, 132, 155, 156, 166, 185, 186, 145, 176]
dianxinlist = [133, 153, 177, 173, 180, 181, 189, 199]
t_list = input("请输入要生成的运营商名称(移动、联通、电信):")
if t_list == "移动":
t_list = yidonglist
elif t_list == "联通":
t_list = liantonglist
elif t_list == "电信":
t_list = dianxinlist
else:
print("请输入有效的运营商名称!!!")
exit()
while True:
top_three = random.choice(t_list)
last_eight = random.sample([str(i) for i in range(10)], 8)
concat_last_eight = "".join(last_eight)
t_set.add(f"{top_three}{concat_last_eight}")
if len(t_set) >= num:
break
return t_set
def start():
num = int(input("请输入你想要生成多少个电话号码:"))
t_set = create_phone(num)
save_to_file = input("是否要将生成的号码保存到文件中?(输入 '是' 或 '否'):")
if save_to_file.lower() == '是':
filenames = input("请输入文件名称:")
with open(filenames, 'w') as file:
for nums in t_set:
file.write(nums + '\n')
print(f"号码已保存到文件 {filenames} 中。")
elif save_to_file.lower() == '否':
print("生成的号码如下:")
for nums in t_set:
print(nums)
else:
print("无效的输入。请输入 '是' 或 '否'。")
start()
import random
def generate_license_plate(province, num_plates):
plates = []
for _ in range(num_plates):
if province == "沪": # 上海车牌
plate_number = f"{province}{random.choice(['A', 'B', 'C', 'D', 'E'])}{random.randint(0, 9)}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'])}{random.randint(1000, 9999)}"
elif province == "京": # 北京车牌
plate_number = f"{province}{random.choice(['A', 'B', 'C', 'E', 'F'])}{random.randint(0, 9)}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'])}{random.randint(10000, 99999)}"
elif province == "粤": # 广东车牌
plate_number = f"{province}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])}{random.choice(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'])}{random.randint(1000, 9999)}"
# 可以继续添加其他省份的车牌生成规则
plates.append(plate_number)
return plates
def main():
province = input("请输入省份简称(例如,沪、京、粤等): ")
num_plates = int(input("请输入要生成的车牌数量: "))
if province:
write_to_file = input("是否要将生成的车牌号码写入文件?(输入 '是' 或 '否'):")
license_plates = generate_license_plate(province, num_plates)
if write_to_file.lower() == '是':
filenames = input("请输入文件名称:")
with open(filenames, 'w',encoding='utf-8') as file:
for plate in license_plates:
file.write(plate + '\n')
print(f"生成的{province}省车牌号码已保存到文件 {filenames} 中。")
elif write_to_file.lower() == '否':
print(f"{province}省生成的车牌号码如下:")
for plate in license_plates:
print(plate)
else:
print("无效的输入。请输入 '是' 或 '否'。")
else:
print("请输入有效的省份简称。")
if __name__ == "__main__":
main()
import string
import random
def create_license(num):
t_set = set()
while True:
t_list = [f"鄂{i}" for i in string.ascii_uppercase.split("T")[0] if i not in ["I","O"]]
top_two = random.choice(t_list)
the_third = random.choice(string.ascii_uppercase)
last_four = random.sample([str(i) for i in range(10)],4)
concat_last_four = "".join(last_four)
t_set.add(f"{top_two}·{the_third}{concat_last_four}")
if len(t_set) >= num:
break
return t_set
def start():
num = int(input("请输入你想要生成多少个车牌:"))
t_set = create_license(num)
save_to_file = input("是否要将生成的号码保存到文件中?(输入 '是' 或 '否'):")
if save_to_file.lower() == '是':
filenames = input("请输入文件名称:")
with open(filenames, 'w',encoding='utf-8') as file:
for nums in t_set:
file.write(nums + '\n')
print(f"生成的湖北省车牌号码已保存到文件 {filenames} 中。")
elif save_to_file.lower() == '否':
print("生成的湖北省车牌号码如下:")
for nums in t_set:
print(nums)
else:
print("无效的输入。请输入 '是' 或 '否'。")
start()