Python之随机数

随机数可以用于数学,游戏,安全等领域中,还经常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。
Python包含以下常用随机数函数:

choice(seq)      # 从序列的元素中随机挑选一个元素,比如random.choice(range(10)) # 从0到9中随机挑选一个整数。
randrange ([start,] stop [,step])     # 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1
random()         # 随机生成下一个实数,它在[0,1)范围内。
seed([x])        # 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。
shuffle(lst)     # 将序列的所有元素随机排序
uniform(x, y)    # 随机生成下一个实数,它在[x,y]范围内。

应用案例

1)choice(seq) 
seq -- 可以是一个列表,元组或字符串

>>> import random
>>>
>>> print("choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]))
choice([1, 2, 3, 5, 9]) :  1
>>> print("choice('A String') : ", random.choice('A String'))
choice('A String') :  i
2)random.randrange ([start,] stop [,step])

start -- 指定范围内的开始值,包含在范围内
stop  -- 指定范围内的结束值,不包含在范围内
step  -- 指定递增基数

>>> import random
>>>
>>> # 输出 100 <= number < 1000 间的偶数
... print("randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2))
randrange(100, 1000, 2) :  434
>>>
>>> # 输出 100 <= number < 1000 间的其他数
... print("randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3))
randrange(100, 1000, 3) :  904
3)random() 
返回随机生成的一个实数,它在[0,1)范围内
>>> import random >>> >>> # 生成第一个随机数 ... print("random() : ", random.random()) random() : 0.8703677949860699 >>> >>> # 生成第二个随机数 ... print("random() : ", random.random()) random() : 0.5672871745528487
4) random.seed ( [x] )
x -- 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed

>>> import random
>>>
>>> random.seed( 10 )
>>> print("Random number with seed 10 : ", random.random())
Random number with seed 10 :  0.5714025946899135
>>>
>>> # 生成同一个随机数
... random.seed( 10 )
>>> print("Random number with seed 10 : ", random.random())
Random number with seed 10 :  0.5714025946899135
>>>
>>> # 生成同一个随机数
... random.seed( 10 )
>>> print("Random number with seed 10 : ", random.random())
Random number with seed 10 :  0.5714025946899135

# 未设置种子
>>> print("Random number with seed 10 : ", random.random())
Random number with seed 10 :  0.4288890546751146
>>> print("Random number with seed 10 : ", random.random())
Random number with seed 10 :  0.5780913011344704
>>> print("Random number with seed 10 : ", random.random())
Random number with seed 10 :  0.20609823213950174
5)shuffle() 
参数:可以是一个序列或者元组
返回值:返回随机排序后的序列

>>> import random
>>>
>>> list = [20, 16, 10, 5];
>>> random.shuffle(list)
>>> print("随机排序列表 : ",  list)
随机排序列表 :  [10, 20, 16, 5]
>>>
>>> random.shuffle(list)
>>> print("随机排序列表 : ",  list)
随机排序列表 :  [10, 16, 5, 20]
6)uniform() 
参数:
x -- 随机数的最小值。
y -- 随机数的最大值。

返回值:返回一个浮点数,可以通过数据转换为整数

>>> import random
>>>
>>> print("uniform(5, 10) 的随机数为 : ",  random.uniform(5, 10))
uniform(5, 10) 的随机数为 :  9.117944362667227
>>>
>>> print("uniform(7, 14) 的随机数为 : ",  random.uniform(7, 14))
uniform(7, 14) 的随机数为 :  11.574307737308231

>>> print("uniform(7, 14) 的随机数转为整数为 : ",  int(random.uniform(7, 14)))
uniform(7, 14) 的随机数转为整数为 :  8
>>> print("uniform(7, 14) 的随机数转为整数为 : ", int(random.uniform(7, 14))) uniform(7, 14) 的随机数转为整数为 : 10

 

posted @ 2016-05-28 16:07  每天进步一点点!!!  阅读(751)  评论(0编辑  收藏  举报