1 #__author:"吉勇佳" 2 #date: 2018/10/14 0014 3 #function: 4 5 import math 6 import random 7 8 # 向上取整 9 print(math.ceil(18.1)) 10 11 ''' 12 輸出: 13 19 14 ''' 15 16 # 向下取整 17 print(math.floor(18.1)) 18 19 ''' 20 輸出: 21 18 22 ''' 23 24 # 返回整數與小數 25 print(math.modf(22.3)) 26 27 ''' 28 輸出 29 (0.3000000000000007, 22.0) 30 ''' 31 32 # 開平方 33 print(math.sqrt(16)) 34 35 ''' 36 返回: 37 4.0 38 ''' 39 40 # 隨機數 41 # 方法1:從序列的元素中隨機取出一個數字來 42 print(random.choice([1,3,4,5,6,7,8,9,4,3,2,22,13,445,3,2,3])) 43 44 # 方法2 :從range范圍內取值 45 print(random.choice(range(1,100))) 46 47 # 方法3:從字符串中隨機取 48 print(random.choice("jiyongjia")) 49 50 # 方法4:從多個字符串的列表中隨機取 51 print(random.choice(["jiyongjia","sunxin","zhanglei "])) 52 53 54 # 方法5:用randrange方法 55 print(random.randrange(1,100,2)) 56 57 # 方法6:取出隨機的一個整形數字 58 print(random.randint(1,100)) 59 60 # 方法7:隨機的0-1之間的數字 61 print(random.random()) 62 63 # 方法8:將序列的元素隨機排序 64 list=[1,2,3,4,5,6,7] 65 random.shuffle(list) 66 print(list) 67 ''' 68 輸出: 69 [1, 4, 2, 5, 3, 6, 7] 70 '''
# 方法9 :隨機產生一個實數 print(random.uniform(3,19)) ''' 輸出: 3.327772693472834 '''