**
**
利用random函数随机生成姓名和手机号,用到的函数主要有random.randint、random.choice、“sep”.join(seq),要改生成的名字、手机号的数量只需要改range()里面的数值就行。
随机生成名字的第一种写法:
#第一种写法
import random
for i in range(1000):
def name():
xing=random.choice(["赵","钱","孙","李","周","吴"])
ming="".join(random.choice(["义","凝","好","九","立","风","离",""])for i in range(1,4))
return xing+ming
newname=name()
print(newname)
#第二种写法
for i in range(10):
xing='赵钱孙李周吴郑王'
ming='人间四月芳菲尽山寺桃花始胜开'
x=random.choice(xing)
m="".join(random.choice(ming) for i in range(1,3))
x+m
print(x+m)
#第三种写法
def name():
xing=random.choice(["赵","钱","孙","李","周","吴"])
ming1=random.choice(["义","凝","好","九","立","风","离",""])
ming2=random.choice(["家","月","集","必","","楼","音"])
return xing+ming1+ming2
for i in range(10):
newname=name()
print(newname)
批量随机生成手机号,注释掉的是另一种随机数的取法。
def phone():
#创建手机号第二位
second=random.choice([3,4,5,7,8,9]) #从这个list里面随机选择一个数字
#创建手机号第三位
third={
3:random.randint(0,9), #从0-9之间随机生成一个整数int类型
4:random.choice([5,7,9]),
#4:[5,7,9][random.randint(0,2)],
5:random.choice([0,1,2,3,4,5,7,8]),
#5:[i for i in range(10) if i !=4][random.randint(0,8)],
7:random.choice([2,3,5,6,7,8]),
#7:[i for i in range(10) if i not in [4,9]][random.randint(0,7)],
8:random.randint(0,9),
9:random.choice([1,3,8,9]),
#9:[1,3,8,9][random.randint(0,2)],
}[second] #third后面加上second是由于third的生成要依赖second的随机选择结果
#创建手机号最后八位
suffix=random.randint(9999999,100000000)
#拼接手机号
return "1{}{}{}".format(second,third,suffix)
#创建批量生成手机号的循环
for i in range(10):
randomphone=phone()
print(randomphone)