Python基础 之 Python random 模块 1

简介: Python random 模块

Python基础 之 Python random 模块 1

Python random 模块

Python random 模块主要用于生成随机数。

random 模块实现了各种分布的伪随机数生成器。

要使用 random 函数必须先导入:

import random

查看 random 模块中的内容:

实例

import random
dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', 'all', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'spec', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_floor', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randbytes', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']

接下来我们使用 random() 方法返回一个随机数,它在半开放区间 [0,1) 范围内,包含 0 但不包含 1。

实例

导入 random 包

import random

生成随机数

print(random.random())

以上实例输出结果为:

0.4784904215869241

目录
相关文章
|
19天前
|
资源调度 算法 Python
「Python系列」Python random模块、hashlib模块
`random` 模块在 Python 中提供了多种生成随机数的方法
30 0
|
7月前
|
Python
|
9月前
|
Python
|
9月前
|
Python
|
9月前
|
Python
|
9月前
|
Python Windows
|
9月前
|
Python
|
9月前
|
Python
|
9月前
|
Python
|
9月前
|
Python