# (1)随机生成100以内的10个整数
[random.randrange(0, 100) for _ in range(10) ]
# (2)随机选取0到100间的整数
random.randint(0,100)
# (3)从字符串“abcdefghijklmn"随机选取4个字符
random.choices('abcdefghijklmn', k=4)
# (4)随机选取列表['apple','pear','peach','orange']中的一个字符串
random.choice(['apple','pear','peach','orange'])
2. linux编写代码实现两个班级的随机点名(学号)
#!/usr/bin/python
importrandom
l=['a','b','c','d','e','f','g','h','i','j','k','l','n','m','o','p','q','r','s','t','u','v','w','x','y','z']
n=len(l)
k=0
printl
foriinrange(n):
k+=1
a=random.randrange(0,len(l))
printa
printl.pop(a)
printl
printk
3. 关于Python中的随机数生成步骤和随机数质量
Python生成随机数和随机数质量的方法,random.random()用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成随机数:
printrandom.uniform(10,20)
printrandom.uniform(20,10)
#----
#18.7356606526
#12.5798298022
random.randint
用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,Python生成随机数
printrandom.randint(12,20)#生成的随机数n:12<=n<=20
printrandom.randint(20,20)#结果永远是20
#printrandom.randint(20,10)#该语句是错误的。
random.randrange方法从指定范围内,按指定基数递增的集合中 ,下面对python生成随机数的应用程序的部分介绍:
1.随机整数:
>>>importrandom
>>>random.randint(0,99)
21
2.随机选取0到100间的偶数:
>>>importrandom
>>>random.randrange(0,101,2)
42
3.随机浮点数:
>>>importrandom
>>>random.random()
0.85415370477785668
>>>random.uniform(1,10)
5.4221167969800881
4.随机字符:
>>>importrandom
>>>random.choice('abcdefg&#%^*f')
'd'
5.多个字符中选取特定数量的字符:
>>>importrandom
random.sample('abcdefghij',3)
['a','d','b']
6.多个字符中选取特定数量的字符组成新字符串:
>>>importrandom
>>>importstring
>>>string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'],3)).r
eplace("","")
'fih'
4. 怎么样用python做个程序!要从列表中抽取随机取
import random
lis = ['%03d'%x for x in range(1,501)]
res = random.sample(lis,50)
for i,item in enumerate(res):
....print("%d :%s"%(i+1,item))
把. 换成缩进
5. Python如何设置六个人当中选出来三个程序
from random import sample
people_list = ['A', 'B', 'C', 'D', 'E', 'F'] #6个人名的列表
choose_three = sample(people_list, 3) #随机选出3个人
print(choose_three) #打印列表
print(*choose_three) #打印列表内单个内容,并用空格隔开
print(*choose_three, sep=' ') #分行打印列表内容
示例输出:
['D', 'F', 'B']
D F B
D
F
B
6. 菜鸟,想做一个随机点名软件,抽取班上同学名字的那种。求方法,具体步骤,需要什么软件做。
VB可以做,什么软件都可以,就是随机数
要点名很容易,随机抽取一名同学即可。但是点名以后的数据记录和分析处理比较复杂。
你可以下载“倪大侠点名计分器”看一看,去“倪大侠软件”官网下载最新版,不断更新中。
7. 如何用python编写一个简易的随机点名软件
思路:1、定义一个字典,把名字和数字对应起来;2、引用随机模块;3、输入要点名的个数,通过循环输出名字。
具体代码如下:
#-*-coding:utf-8-*-
fromrandomimportrandint
DictName={1:'Aaron',
2:'Abel',
3:'Abraham',
4:'Adam',
5:'Adrian',
6:'Alva',
7:'Alex',
8:'Alexander',
9:'Alan',
10:'Albert',
11:'Alfred',
12:'Andrew',
13:'Andy',
14:'Angus',
15:'Anthony',
16:'Arthur',
17:'Austin',
18:'Ben',
19:'Benson',
20:'Bill',
21:'Bob',
22:'Brandon',
23:'Brant',
24:'Brent',
25:'Brian',
26:'Bruce',
27:'Carl',
28:'Cary',
29:'Caspar',
30:'Charles',
31:'Cheney',
32:'Chris',
33:'Christian',
34:'Christopher',
35:'Colin',
36:'Cosmo',
37:'Daniel',
38:'Dennis',
39:'Derek',
40:'Donald',
41:'Douglas',
42:'David',
43:'Denny',
44:'Edgar',
45:'Edward',
46:'Edwin',
47:'Elliott',
48:'Elvis',
49:'Eric',
50:'Evan',
51:'Francis',
52:'Frank',
53:'Franklin',
54:'Fred',
55:'Gabriel',
56:'Gaby',
57:'Garfield',
58:'Gary'}
Num=raw_input('请输入需要点名个数:')
flag=True
whileflag:
ifNum.isdigit():
Ind=int(Num)
flag=False
else:
print'输入错误,请确认'
else:
foriinrange(Ind):
printDictName[randint(1,58)]
运行结果:
请输入需要点名个数:4
Brant
Cheney
David
Alan
8. Python 生成随机点坐标
importrandom
importnumpyasnp
List=np.array([(0,0),(1,1),(1.6,1.8),(3,3)])
d=0.5
defget_random(low,high):
return((high-low)*random.random()+low)
n=0
whilen<100000:
x=get_random(0,3)
y=get_random(0,3)
rand_tuple=np.array([x,y])
tmp_dist=np.sqrt(np.sum(np.square(List-rand_tuple),axis=1))
tmp_dist_bool=tmp_dist>=d
ifnp.sum(tmp_dist_bool)==len(List):
print(x,y)
break
n+=1
ifn==100000:
print("After",n,"tries,can'tgetarandompoint!!")
9. python 知道直线方程,怎么在直线上随机生成一个点
知道直线方程,在直线上随机生成一个点的原理和程序,以及运行结果见图
10. 请教如何用Python完成一个将老师随机分配到办公室的程序。
#teachers=['a','b','c','d','e','f','g','h','j','k','m']
#offices=[[],[],[],[]]
#要求是将11名老师随机分配到4个办公室,每个办公室保证至少分配两名老师。
importrandom
teachers=['a','b','c','d','e','f','g','h','j','k','m']
offices=[[],[],[],[]]
classOffice:
def__init__(self,num):
self.teachers_list=[]
self.num=num
defadd(self,x):
self.teachers_list.append(x)
defret(self):
returnself.teachers_list
def__str__(self):
returnstr(self.num)
#调用系统时间,实现随机数
random.seed()
#一共3种情况:
#3332=11
#4232=11
#5222=11
case_index=random.randrange(1,4)
offices_list=[]
ifcase_index==1:
#3332
forein[3,3,3,2]:
offices_list.append(Office(e))
elifcase_index==2:
#4232
forein[4,3,2,2]:
offices_list.append(Office(e))
else:
#5222
forein[5,2,2,2]:
offices_list.append(Office(e))
#打乱顺序
random.shuffle(offices_list)
print("办公室随机分配名额如下:")
forofficeinoffices_list:
print(office,end="")
print()
print("开始分配老师:")
#分配老师
forteacherinteachers:
whileTrue:
index=random.randrange(0,len(offices))
office=offices_list[index]
iflen(office.teachers_list)>=office.num:
continue
office.add(teacher)
break
foriinrange(len(offices_list)):
office=offices_list[i]
offices[i]=office.ret()
print(offices[i])
可以运行!请指教!