random.choice函数
import random
print("the number is",random.choice([1,2,3,4,5,6,5,6,9,6,7,]))
输出结果:
the number is 6
the number is 9
the number is 7
the number is 2
the number is 6
the number is 5
每次运行该程序得出的结果都是随机的
random.randrange模块
import math # 输出 100 <= number < 1000 间的偶数 print "the number is ", random.randrange(100, 1000, 2) # 输出 100 <= number < 1000 间的其他数 print "the number is: ", random.randrange(100, 1000, 3)
random.random函数:在0-1之间随机生成一个数
import random # 生成第一个随机数 print "random() : ", random.random() # 生成第二个随机数 print "random() : ", random.random()
shuffle() 方法将序列的所有元素随机排序
import random list = [20, 16, 10, 5] random.shuffle(list) print "随机排序列表 : ", list
uniform() 方法将随机生成下一个实数,它在 [x, y] 范围内。
import random print "uniform(5, 10) 的随机数为 : ", random.uniform(5, 10) print "uniform(7, 14) 的随机数为 : ", random.uniform(7, 14)
运行结果:
uniform(5, 10) 的随机数为 : 6.98774810047
uniform(7, 14) 的随机数为 : 12.2243345905
print "sin(3) : ", math.sin(3) print "cos(3) : ", math.cos(3) print "tan(3) : ", math.tan(3)
acos(x) 返回x的反余弦弧度值。
asin(x) 返回x的反正弦弧度值。
atan(x) 返回x的反正切弧度值。
atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。
cos(x) 返回x的弧度的余弦值。
hypot(x, y) 返回欧几里德范数 sqrt(xx + yy)。
sin(x) 返回的x弧度的正弦值。
tan(x) 返回x弧度的正切值。
degrees(x) 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
radians(x) 将角度转换为弧度
都是可以直接导入变量x的



