導航:首頁 > 編程語言 > python隨機點名程序

python隨機點名程序

發布時間:2022-04-12 00:12:31

1. python怎麼編程這個程序

# (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

print
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])

可以運行!請指教!

閱讀全文

與python隨機點名程序相關的資料

熱點內容
網吧怎麼租伺服器 瀏覽:274
ansys畫圓命令流 瀏覽:774
騰訊雲盤伺服器地址 瀏覽:762
無損壓縮可以壓文檔嘛 瀏覽:115
人工智慧編譯視頻 瀏覽:525
什麼新聞app比較真實 瀏覽:348
自製編譯器自製編程語言 瀏覽:112
python常態開發 瀏覽:134
復制加密卡到榮耀手錶 瀏覽:680
dellemc伺服器如何安裝系統 瀏覽:706
python爬取整個網頁 瀏覽:665
程序員搞笑圖片上線 瀏覽:777
杜pdf 瀏覽:234
小米直播伺服器地址 瀏覽:488
redhatlinux服務 瀏覽:549
APP頁面頂端展示畫面叫什麼 瀏覽:821
python兩點間距離 瀏覽:184
新程序員珠海和武漢哪個適合發展 瀏覽:276
爬取vip視頻python代碼 瀏覽:669
單片機中trisd什麼意思 瀏覽:130