【python 随机数】Python里面如何生成随机数?

主要知识点:
随机整数:

random.randint(a,b):返回随机整数x,a<=x<=b

包含范围的随机整数
random.randrange(start,stop,[,step]):返回一个范围在(start,stop,step)之间的随机整数,不包括结束值。

0-1之间的随机浮点数
随机实数:random.random( ):返回0到1之间的浮点数

指定范围的随机浮点数
random.uniform(a,b):返回指定范围内的浮点数。

实战演示:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (
AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> print random.randint(1,3)
2
>>> print random.randint(1,3)
1
>>> print random.randint(1,3)
2
>>> print random.randint(1,3)
2
>>> print random.randint(1,10)
1
>>> print random.randint(1,10)
10
>>> print random.randint(1,10)
5
>>> print random.randint(1,10)
1
>>> print random.randint(1,10)
5
>>> print random.randint(1,10)
7
>>> print random.randint(1,10)
4
>>> print random.randint(1,10)
3
>>> print random.randrange(1,5,1)
2
>>> print random.randrange(1,5,1)
4
>>> print random.randrange(1,5,1)
4
>>> print random.randrange(1,5,1)
4
>>> print random.randrange(1,5,1)
3
>>> print random.randrange(1,5,1)
1
>>> print random.randrange(1,5,1)
3
>>> print random.randrange(1,5,1)
1
>>> print random.randrange(1,5,1)
1
>>> print random.randrange(1,5,1)
2
>>> print random.randrange(1,5,1)
1
>>> print random.randrange(1,5,1)
1
>>> print random.randrange(1,5,1)
4
>>> print random.random()
0.395644101608
>>> print random.random()
0.939752210993
>>> print random.random()
0.753840174814
>>> print random.random()
0.6284505895
>>> print random.random()
0.221124079082
>>> print random.random()
0.859997827515
>>> print random.uniform(3,5)
3.68718533467
>>> print random.uniform(3,5)
3.44178711732
>>> print random.uniform(3,5)
3.66400073252
>>> print random.uniform(3,5)
4.90505268541
>>> print random.uniform(3,5)
4.69546962717
>>> print random.uniform(3,5)
4.26153330638
>>>
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页