res = 0 for i in range(10000000): re += 1 time.sleep(5) t_1_end = time.time() t_2_end = time.perf_counter() t_3_end = time.process_time() print("time方法:{:.3f}秒".format(t_1_end-t_1_start)) print("perf_counter方法:{:.3f}秒".format(t_2_end-t_2_start)) print("process_time方法:{:.3f}秒".format(t_3_end-t_3_start))
from random import * print(random()) # 如果不设置种子则以系统时间为默认值 seed(10) # 相同种子产生的随机数相同 print(random()) seed(10) print(random())
2)产生随机整数
1 2 3 4 5 6 7
from random import * # randint(a,b)——产生[a,b]之间的随机整数 numbers1 = [randint(1,10) for i in range(10)] # randrange(a)——产生[0,a)之间的随机整数 numbers2 = [randrange(10) for i in range(10)] # randrange(a,b,step)——产生[a,b)之间以step为步长的随机整数 numbers2 = [randrange(0,10,2) for i in range(10)]
3)产生随机浮点数
1 2 3 4 5
from random import * # random()——产生[0.0,1.0)之间的随机浮点数 numbers4 = [random() for i in range(10)] # uniform(a,b)——产生[a,b]之间的随机浮点数 numbers2 = [uniform(2.1,3.5) for i in range(10)]