# *#【Python】【基础知识】【模块】【random】【使用random创造一个随机数】

Random介绍： 输出随机数。 快照： #!/usr/bin/python # -*- coding: UTF-8 -*- import random

Random介绍：

```#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random

#生成 10 到 20 之间的随机数
print(random.uniform(10, 20))                      #输出1-20间的随机数(包含整数、小数)
print(random.random())                             #输出0-1间的随机数
print(random.randint(10, 20))                      #输出10-20间的随机整数
print(random.choice([10,11,12,13,14,15]))          #随机输出序列内的一个元素
print(random.choice([x for x in range(1,100)]))    #输出1-99间的随机数```

random模块的方法有：

```>>> dir(random)
[‘BPF‘, ‘LOG4‘, ‘NV_MAGICCONST‘, ‘RECIP_BPF‘, ‘Random‘, ‘SG_MAGICCONST‘, ‘SystemRandom‘, ‘TWOPI‘, ‘_BuiltinMethodType‘, ‘_MethodType‘, ‘_Sequence‘, ‘_Set‘, ‘__all__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘_acos‘, ‘_bisect‘, ‘_ceil‘, ‘_cos‘, ‘_e‘, ‘_exp‘, ‘_inst‘, ‘_itertools‘, ‘_log‘, ‘_os‘, ‘_pi‘, ‘_random‘, ‘_sha512‘, ‘_sin‘, ‘_sqrt‘, ‘_test‘, ‘_test_generator‘, ‘_urandom‘, ‘_warn‘, ‘betavariate‘, ‘choice‘, ‘choices‘, ‘expovariate‘, ‘gammavariate‘, ‘gauss‘, ‘getrandbits‘, ‘getstate‘, ‘lognormvariate‘, ‘normalvariate‘, ‘paretovariate‘, ‘randint‘, ‘random‘, ‘randrange‘, ‘sample‘, ‘seed‘, ‘setstate‘, ‘shuffle‘, ‘triangular‘, ‘uniform‘, ‘vonmisesvariate‘, ‘weibullvariate‘]```

# uniform 方法的使用：

```>>> dir(random.uniform)
[‘__call__‘, ‘__class__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__func__‘, ‘__ge__‘, ‘__get__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__le__‘, ‘__lt__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__self__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘]
>>> help(random.uniform)
Help on method uniform in module random:

uniform(a, b) method of random.Random instance
Get a random number in the range [a, b) or [a, b] depending on rounding.```

random的uniform(a,b) 方法。

random例子：

在[a,b)  或[a,b]  范围内，根据四舍五入，获取一个随机数。

使用random创建一个随机数；

```#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random

#生成 10 到 20 之间的随机数
print(random.uniform(10, 20))```

结果精确到小数点后14位；

# choice方法的使用

random.choice(seq)

其参数为序列，从序列中随机获取一个随机元素；

示例：

```>>> random.choice([1,2,3,4,5,6,7,3])
2
>>> random.choice([1,2,3,4,5,6,7,3])
2
>>> random.choice([1,2,3,4,5,6,7,3])
5
>>> random.choice([1,2,3,4,5,6,7,3])
4
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
4
>>> random.choice([1,2,3,4,5,6,7,3])
6
>>> random.choice([1,2,3,4,5,6,7,3])
6
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
5
>>> random.choice([1,2,3,4,5,6,7,3])
6
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
3
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
5
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice([1,2,3,4,5,6,7,3])
1
>>> random.choice("test")
‘t‘
>>> random.choice("test")
‘t‘
>>> random.choice("test")
‘t‘
>>> random.choice("test")
‘e‘
>>> random.choice("test")
‘t‘
>>> random.choice("test")
‘s‘
>>> random.choice("test")
‘e‘```

————————（我是分割线）————————

1. RUNOOB.COM：https://www.runoob.com/python/python-exercise-example50.html

2.Python DOC