Python Module_sys/random

简介: 目录目录前言软件环境Python标准库初识Python常用的标准库模块dir 函数使用方法sys操作系统功能模块sysstdinsysstdoutsysstderr标准IOError流sysstdin标准输入流sysstdout标准输出流s...

目录

前言

即便Python多应用于编写脚本程序,但是不能将Python和脚本语言划上等号。Python作为标准的OOP编程语言,其可以实现如Openstack这样的大型系统。在这些大型系统中的实现中,会大量的对类和函数进行自定义和调用,同时Python也提供了有一套非常有用的标准库,它会连同Python解析器一起安装到电脑中。本篇主要来介绍Python的标准库。

软件环境

  • 系统
    • UbuntuKylin 14.04
  • 软件
    • Python 2.7.6
    • IPython 4.0.0

Python标准库初识

Python标准库可以理解为Python自身已有的类和函数。这些类和函数是经过Python创造者们的分析和设计的,能很好的满足开发人员的不同需求,以此来为开发人员提供便利,所以熟悉Python标准库是非常有必要。
Python标准库的安装位置:在Linux中/usr/lib目录下包含了操作系统中应用软件的函数库、目标文件(Object file),以及不被一般用户惯用的执行文件或脚本文件。某些软件会提供一些特殊的命令来进行服务器的设置,这些命令也不会经常被系统管理员操作,那就会被摆放在这个目录下。所以Python会默认将Python2.7/标准库文件安装到该目录下,使用ls命令来列出Python庞大的标准库目录。

In [26]: cd /usr/lib/python2.7/
/usr/lib/python2.7

Python常用的标准库模块

Python标准库中有大量的内置模块、函数,模块中又有许多函数和属性,实质上Python模块就是包含有很多自定义的函数、类以及属性等元素的.py文件。当我们使用import语句导入一个Python模块到运行空间后,就可以访问其内部包含的任意的类、函数及属性。通常我们通过下面几个方法来学习模块的使用方法。
1. ModuleName.__doc__:获取模块的文档
2. dir(ModuleName):获取模块中定义了的名称(属性、方法)的列表
3. help(ModuleName):获取模块的详细帮助手册

dir( )函数使用方法

其中dir()是很常用的一个内置函数,因为Python内置的函数非常多,程序员不可能全部都记住,所以dir()函数可以帮助我们查看对象内定义的属性和方法等名称的列表。而且在Python中一切皆对象,对象都有自己的属性和方法,只要是对象我们就可以使用dir()函数来获取有用的信息。所以除了可以查看Module外,dir()还可以查看变量、函数、类等的名称列表。
dir()文档:不带参数时,返回当前范围内的变量、方法和定义的类型的列表;带参数时,返回参数的属性、方法的列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
例如:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'atexit', 'sys']
>>> import random
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'atexit', 'random', 'sys']
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hashlib', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>>

其中也可以根据Python的命名习惯来了解每一个名称的分类和作用。
a. ‘_X_’ 为系统变量,有特殊含义
b. ‘__X’ 表示类的本地变量,在类中调用(注意:是两条下划线’__’)
c. ‘__X__’ Python Module的内置方法
d. ’ _ ’ 存储着当前最后一个表达式的结果

sys:操作系统功能模块

In [33]: dir(sys)
Out[33]: 
['__displayhook__',
 '__doc__',
 '__egginsert',
 '__excepthook__',
 '__name__',
 '__package__',
 '__plen',
 '__stderr__',
 '__stdin__',
 '__stdout__',
 '_clear_type_cache',
 '_current_frames',
 '_getframe',
 '_mercurial',
 '_multiarch',
 'api_version',
 'argv',
 'builtin_module_names',
 'byteorder',
 'call_tracing',
 'callstats',
 'copyright',
 'displayhook',
 'dont_write_bytecode',
 'exc_clear',
 'exc_info',
 'exc_type',
 'excepthook',
 'exec_prefix',
 'executable',
 'exit',
 'exitfunc',
 'flags',
 'float_info',
 'float_repr_style',
 'getcheckinterval',
 'getdefaultencoding',
 'getdlopenflags',
 'getfilesystemencoding',
 'getprofile',
 'getrecursionlimit',
 'getrefcount',
 'getsizeof',
 'gettrace',
 'hexversion',
 'last_traceback',
 'last_type',
 'last_value',
 'long_info',
 'maxint',
 'maxsize',
 'maxunicode',
 'meta_path',
 'modules',
 'path',
 'path_hooks',
 'path_importer_cache',
 'platform',
 'prefix',
 'ps1',
 'ps2',
 'ps3',
 'py3kwarning',
 'pydebug',
 'setcheckinterval',
 'setdlopenflags',
 'setprofile',
 'setrecursionlimit',
 'settrace',
 'stderr',
 'stdin',
 'stdout',
 'subversion',
 'version',
 'version_info',
 'warnoptions']

sys.stdin\sys.stdout\sys.stderr标准I/O、Error流

sys.stdin\sys.stdout\sys.stderr都是类文件对象(file-like object(stream)),同时也是内建在系统中的管道。

In [275]: %pycat stdTest.py
import sys
for i in [sys.stdin,sys.stdout,sys.stderr]:
    print i

In [276]: run stdTest.py
<open file '<stdin>', mode 'r' at 0x7f7985fae0c0>
<open file '<stdout>', mode 'w' at 0x7f7985fae150>
<open file '<stderr>', mode 'w' at 0x7f7985fae1e0>

sys.stdin标准输入流

当需要输入数据时,结果通过stdin管道。
当我们需要输入数据时,一般使用raw_input(‘String’),这个语句本质上也是调用了sys.stdin.readline()的接口。

In [187]: %pycat stdiTest.py
#!/usr/bin/env python
import sys
print 'My name is %s' % raw_input("What's your name ?")
print 'Ple enter your name:',
name = sys.stdin.readline()[:-1]  #将'\n'过滤
print 'You name is %s' % name

In [188]: run stdiTest.py
What's your name ?JMILK
My name is JMILK
Ple enter your name:jmilk
 You name is jmilk

可见raw_input()和sys.stdin.readline()效果是一致的。

sys.stdout标准输出流

当需要打印时,结果通过stdout管道。
当我们使用print语句进行打印时,实际上在需要打印的String后面追加了’\n’硬回车后,再调用了sys.stdout.write()的接口来进行打印。在Python中的所有对象都必须先经过sys.stdout进行流式化转换后才能够被打印或从定向到别的文件中,而且sys.stdout具有行间缓存的机制,相邻行间的print的内容会缓存到一起,直到没有相邻的print语句后再一起输出。
需要注意的是:sys.stdout可以实现重定向输入
这时候我们需要使用负责文件操作的open()内置函数(使用dir(__builtin__)查看__builtin__模块内的内置函数),下面是一个例子:

In [59]: pycat stdoTest.py
#!/usr/bin/env python
#Filename:stdoTest.py
#coding=utf8
import sys


fristOut = sys.stdout  #备份初始的输出文件对象
print type(fristOut)

logOut = open('/usr/local/src/pyScript/out.log','w') 
sys.stdout = logOut  #重定向输出到新的文件对象
print 'Test stdout.'  #重定向后,不会打印到屏幕

logOut.close()   #关闭open()打开的文件对象
sys.stdout = fristOut  #还原输出文件对象

In [60]: run stdoTest.py
<type 'file'>

In [61]: cat out.log
Test stdout.

open()函数以写模式打开一个文件,并返回一个文件对象。若文件不存在会自动创建新的同名文件。注意:在使用open()函数打开一个文件后,一定要调用文件对象的close()方法来关闭(单向I/O,保持数据的一致性)。说到底套路就是:将一个使用open()函数打开的文件对象赋值给sys.stdout文件对象,接下来的print语句都会重定向到新的sys.stdout文件对象指向的文件中,最后再调用close()函数来关闭打开的文件对象。期间要注意初始标准输出的文件对象的还原。

sys.stderr标准错误输出流

当程序崩溃并需要打印调试信息时,结果通过stderr管道。
向标准错误输出打印错误信息,除了一般的自动输出外,还可以使用下面的方法来即时到处错误信息到sys.stderr

In [71]: %%file stdeTest.py
   ....: #!/usr/bin/env python
   ....: #Filename:stdeTest.py
   ....: #coding=utf8
   ....: import sys
   ....: print >> sys.stderr,'Test stderr'
   ....: 
Writing stdeTest.py

In [72]: run stdeTest.py
Test stderr

这是应用了print语句的快捷语法’>>’,这个语法可以即时打印到任何open()的文件对象或类文件对象中。

In [106]: pycat prtTest.py
#!/usr/bin/env python
import sys
testPrtFile = open('/usr/local/src/pyScript/testPrtFile','w')
print type(testPrtFile)
print >> testPrtFile,'Test print syntax'
testPrtFile.close()

In [107]: run prtTest.py
<type 'file'>

In [110]: cat testPrtFile
Test print syntax

同样的sys.stderr可以实现重定向输出错误信息。

root@Jmilk:~# cat stdeTest.py 
#!/usr/bin/env python
#Filename:stdeTest.py
#coding=utf8
import sys

errOut = sys.stderr
errFile = open('errFile','w')
print type(errFile)
sys.stderr = errFile
raise Exception,"Test Stderr"
sys.stderr = errOut
stdeFile.close()
root@Jmilk:~# python stdeTest.py 
<type 'file'>
root@Jmilk:~# cat errFile 
Traceback (most recent call last):
  File "stdeTest.py", line 10, in <module>
    raise Exception,"Test Stderr"
Exception: Test Stderr

注意:在sys.stderr的重定向中可以不作关闭文件对象的操作,也可以不还原stderr文件对象,因为在Python程序发生异常的时候,Python会自动的帮助我们清理和关闭文件。一旦程序发生异常,那么程序也随之结束了。即将上面的例子修改成如下:

#!/usr/bin/env python
#Filename:stdeTest.py
#coding=utf8
import sys

errFile = open('errFile','w')
sys.stderr = errFile
raise Exception,"Test Stderr"

sys.argv执行脚本命令行附加参数

当Python解析器执行一个PythonScript时,会将执行命令行中的参数传递给argv[]列表,其中argv列表的第一个参数argv[0]为执行文件名。

In [169]: %pycat argvTest.py
#/usr/bin/env python
#Filename:argvTest.py
#coding=utf8
import sys
print sys.argv
print sys.argv[:1]
print len(sys.argv)
n = len(sys.argv)
while True:
    if n > 1:
        for para in sys.argv[::-1]:
             print 'The parameter list[%d] is %s' % (n,para)
             n -= 1
    else:
        print 'The file name is:%s' % sys.argv[0]
        break

In [170]: run argvTest.py Jmilk
['argvTest.py', 'Jmilk']
['argvTest.py']
2
The parameter list[2] is Jmilk
The parameter list[1] is argvTest.py
The file name is:argvTest.py

上面的例子可以看出,sys.argv会返回一个PythonScript执行命令的参数列表类型的对象,其中sys.argv[0] == Filename,同时因为返回对象类型为List,所以也可以使用len()函数来获取List的长度,既List中元素的个数,而且List作为一个有序序列容器,也使用作为for循环中的迭代对象。值得注意的是在序列中sequence[::1]表示将sequence中的元素的顺序反转。
注意:当以标准输入的方式读入脚本的时候,argv[0]为空值,如下:

root@Jmilk:/usr/local/src/pyScript# python < /usr/local/src/pyScript/argvTest.py 
['']
['']
1
The file name is:

当使用了python -c “ScriptCode”选项,表示将脚本以字符串的形式传递给python解析器。可以直接在后面跟上Script代码,此时argv[0] == ‘-c’

root@Jmilk:/usr/local/src/pyScript# python -c "import sys;print sys.argv[0];print sys.argv[1]" jmilk
-c
jmilk

sys.exit([arg])退出当前程序

exit([arg])终止正在运行的程序并且始终返回一个可选参数的异常SystemExit(status),其中[arg]默认为0,表示正常退出。非0(1-127)表示执行不正常,会抛出异常供捕获来进行额外的操作。而且[arg]也可以为一个对象,如String,会被打印或输出到系统。需要注意的是,sys.exit()也会抛出异常,可以使用help(sys.exit)来查看函数的使用手册。

Help on built-in function exit in module sys:

exit(...)
    exit([status])

    Exit the interpreter by raising SystemExit(status).
    If the status is omitted or None, it defaults to zero (i.e., success).
    If the status is an integer, it will be used as the system exit status.
    If it is another kind of object, it will be printed and the system
    exit status will be one (i.e., failure).

一个抛出异常的例子:

In [16]: %pycat exitTest.py
#/usr/bin/env python
#Filename:argvTest.py
#coding=utf8
import sys
print sys.argv
print sys.argv[:1]
print len(sys.argv)
n = len(sys.argv)
while True:
    if n > 1:
        for para in sys.argv[::-1]:
             print 'The parameter list[%d] is %s' % (n,para)
             n -= 1
    else:
        print 'The file name is:%s' % sys.argv[0]
        sys.exit(1)

In [17]: run exitTest.py
['exitTest.py']
['exitTest.py']
1
The file name is:exitTest.py
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

将break语句换成sys.exit(1)后,会在退出程序的同时抛出一个异常供捕获,如果不不会就会出现上面的警告。
注意:因为sys.exit([arg])返回一个SystemExit异常后,若没有捕获异常Pthon解析器会直接退出,若是捕获异常就可以做一些额外的操作。捕获异常有下面两种处理的方法:
方法1:使用try异常捕获语句

In [117]: cat exitTest.py
#/usr/bin/env python
#Filename:exitTest.py
#coding=utf8
import sys
print sys.argv
print sys.argv[:1]
print len(sys.argv)

n = len(sys.argv)

def exitfunc(value):
    print 'the failed code is %s' % value
    sys.exit()

while True:
    if n > 1:
        for para in sys.argv[::-1]:
             print 'The parameter list[%d] is %s' % (n,para)
             n -= 1
    else:
        print 'The file name is:%s' % sys.argv[0]
    try:
        sys.exit('TestExit')
    except SystemExit,value:
        exitfunc(value)

In [118]: run exitTest.py jmilk
['exitTest.py', 'jmilk']
['exitTest.py']
2
The parameter list[2] is jmilk
The parameter list[1] is exitTest.py
the failed code is TestExit

方法二:使用sys.exitfunc
可以由用户指定退出程序时,所进行的清理操作。这个操作应该是一个无参数的函数,当Python解析器退出时,这个函数将会被调用。
sys.exitfunc官方文档:
This value is not actually defined by the module, but can be set by the user (or by a program) to specify a clean-up action at program exit. When set, it should be a parameterless function. This function will be called when the interpreter exits. Only one function may be installed in this way; to allow multiple functions which will be called at termination, use the atexit module.

Note:The exit function is not called when the program is killed by a signal, when a Python fatal internal error is detected, or when os._exit() is called.
Deprecated since version 2.4: Use atexit instead.

#指定一个退出操作函数
sys.exitfunc = newexitfunc
#指定并执行一个退出操作函数
sys.exitfunc = newexitfunc()

sys.paltform OS标识符

可以获取当前操作系统的标识符,

In [174]: osType = sys.platform

In [175]: osType
Out[175]: 'linux2'

其中LinuxOS有’Linux’和’Linux2’两种标识符。可以获取不同系统的标识符,并以此作为判断条件,实现在不同的操作系统中实现不同的操作。

sys.modules 模块目录字典

sys.modules字典包含了所有已经加载的模块,每一个键值对都是模块名称到载入模块的映射。import语句首先会检查sys.modules获取模块的目录路径。Python解析器启动时便会加载需要模块。

print sys.modules.keys()

可以查看当前已经加载的模块的名称。

sys.path 模块目录列表

sys.path相当于系统中的环境变量,sys.path是一个存放着Python源模块、packages、编译模块、二进制扩展文件的路径的目录列表。在Python程序中导入一个模块时,会依次到sys.path所提供的文件目录列表中搜索。当Python解析器启动的时,sys.path会根据内建规则,PYTHONPATH变量的内容来初始化sys.path的值。而且因为sys.path就是一个List类型的对象,所以可以对其进行所有列表支持的操作。

In [179]: sys.path
Out[179]: 
['',
 '/usr/local/bin',
 '/opt/stack/keystone',
 '/opt/stack/swift',
 '/opt/stack/glance',
 '/opt/stack/cinder',
 '/opt/stack/neutron',
 '/opt/stack/nova',
 '/opt/stack/horizon',
 '/opt/stack/ceilometer',
 '/opt/stack/heat',
 '/opt/stack/trove',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/local/lib/python2.7/dist-packages/IPython/extensions',
 '/root/.ipython']

sys.builtin_module_names Python内建模块

sys.builtin_module_names是一个包含了所有Python内建模块名称的Tuple类型对象。

In [182]: sys.builtin_module_names
Out[182]: 
('__builtin__',
 '__main__',
 '_ast',
 '_bisect',
 '_codecs',
 '_collections',
 '_functools',
 '_heapq',
 '_io',
 '_locale',
 '_md5',
 '_random',
 '_sha',
 '_sha256',
 '_sha512',
 '_socket',
 '_sre',
 '_struct',
 '_symtable',
 '_warnings',
 '_weakref',
 'array',
 'binascii',
 'cPickle',
 'cStringIO',
 'cmath',
 'errno',
 'exceptions',
 'fcntl',
 'gc',
 'grp',
 'imp',
 'itertools',
 'marshal',
 'math',
 'operator',
 'posix',
 'pwd',
 'select',
 'signal',
 'spwd',
 'strop',
 'sys',
 'syslog',
 'thread',
 'time',
 'unicodedata',
 'xxsubtype',
 'zipimport',
 'zlib')

一个使用sys.builtin_module_namesde的小例子

#!/usr/bin/env python
#Filename:testBuiltinMo.py
#coding=utf8
import sys

def dump(module):
    print module,"==>",
    if module in sys.builtin_module_names:
        print ""
    else:
        module = __import__(module)
        print module,
        print type(module),
        print module.__file__

dump("os")
dump("sys")
dump("string")
dump("strop")
dump("zlib")

In [229]: run jmilk.py
os ==> <module 'os' from '/usr/lib/python2.7/os.pyc'> <type 'module'> /usr/lib/python2.7/os.pyc
sys ==> 
string ==> <module 'string' from '/usr/lib/python2.7/string.pyc'> <type 'module'> /usr/lib/python2.7/string.pyc
strop ==> 
zlib ==> 

上面的例子将不包含在内建模块Tuple元素中的模块的路径映射输出。
Python的import语句实际上是调用了内建的__import__()函数。__import__()函数只接受String类型参数,其最大的好处是可以在Python程序中动态的导入模块。将需要导入的模块的名字赋值给一个String类型的变量,再传参给__import__()函数就可以很方便的实现动态调用。而且如果自定义的模块的名字中带有‘-’之类的符号时,例如:

import example-test

这样将无法运行。使用下面的方法:

module = __import__('example-test')

random:随机数模块

In [35]: dir(random)
Out[35]: 
['BPF',
 'LOG4',
 'NV_MAGICCONST',
 'RECIP_BPF',
 'Random',
 'SG_MAGICCONST',
 'SystemRandom',
 'TWOPI',
 'WichmannHill',
 '_BuiltinMethodType',
 '_MethodType',
 '__all__',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 '_acos',
 '_ceil',
 '_cos',
 '_e',
 '_exp',
 '_hashlib',
 '_hexlify',
 '_inst',
 '_log',
 '_pi',
 '_random',
 '_sin',
 '_sqrt',
 '_test',
 '_test_generator',
 '_urandom',
 '_warn',
 'betavariate',
 'choice',
 'division',
 'expovariate',
 'gammavariate',
 'gauss',
 'getrandbits',
 'getstate',
 'jumpahead',
 'lognormvariate',
 'normalvariate',
 'paretovariate',
 'randint',
 'random',
 'randrange',
 'sample',
 'seed',
 'setstate',
 'shuffle',
 'triangular',
 'uniform',
 'vonmisesvariate',
 'weibullvariate']

random.random() 生成[0.0,1.0)的随机浮点数

In [81]: random.random()
Out[81]: 0.7249224154828148

可以使用下面的方法来获取各种区间的随机数([0,1)*区间+1)

In [101]: int(random.random()*100+1)
Out[101]: 85

也可以使用random.randint(x,y)来实现这种效果

random.randint(x,y)生成(x,y)指定区间的随机整数

x < y ⇒ [x,y]
x = y ⇒ x
x > y ⇒ ValueError: empty range for randrange()

In [106]: random.randint(1,100)
Out[106]: 75

random.unfiorm(x,y)生成(x,y)之间的随机浮点数

moreNumber <= n <= smallerNumber
x = y ⇒ x

In [109]: random.uniform(10,20)
Out[109]: 10.957028393276119

In [110]: random.uniform(20,10)
Out[110]: 13.838192427153727

random.randrange(start,stop,[step])生成一个指定步进的随机整数

在一个范围内以指定的步进step随机抽取一个整数,其中step缺省为1

In [111]: random.randrange(10,100,10)
Out[111]: 60

In [112]: random.randrange(10,100,10)
Out[112]: 10

random.choice(sequence)随机抽取序列类型中的一个元素

从一个有序的序列中随机抽取一个元素

In [133]: number = [1,11,21,31,41,51]

In [135]: random.choice(number)
Out[135]: 51

In [140]: name = 'Jmilk'

In [143]: random.choice(name)
Out[143]: 'J'

其中的sequence为一个List、String、Tuple等序列类型,详细请参考:
http://blog.csdn.net/jmilk/article/details/48391283

random.sample(sequence,n)生成随机从sequence中抽取n个元素组合的新的List对象。

随机抽取sequence中n个元素,并返回一个由这些元素组合成的新的List对象,不会改变原有的序列对象

In [159]: random.sample(name,2)
Out[159]: ['J', 'l']

In [160]: random.sample(name,2)
Out[160]: ['m', 'l']

random.shuffle(List)打乱一个List对象中元素的顺序

In [178]: help(random.shuffle)

shuffle(self, x, random=None) method of random.Random instance
    x, random=random.random -> shuffle list x in place; return None.

用于将一个List对象中的元素的顺序打乱,不会返回新的对象,改变原来大的List的对象。

In [181]: random.shuffle(number)

In [182]: number
Out[182]: [11, 31, 1, 51, 21, 41]

最后

除了上面介绍的两个Module,Python还有许多诸如os、commands等有用的模块,在往后的博文中会逐一的介绍。

Jmilk

相关文章
|
3月前
|
资源调度 算法 JavaScript
Python基础专题 - 超级详细的 Random(随机)原理解析与编程实践
Python基础专题 - 超级详细的 Random(随机)原理解析与编程实践
100 0
|
6月前
|
XML Shell Linux
python内置模块(random、os、sys、shelve、configparser、xml)
python内置模块(random、os、sys、shelve、configparser、xml)
45 0
|
5月前
|
算法 安全 量子技术
【Python】蒙特卡洛模拟 | PRNG 伪随机数发生器 | 马特赛特旋转算法 | LCG 线性同余算法 | Python Random 模块
【Python】蒙特卡洛模拟 | PRNG 伪随机数发生器 | 马特赛特旋转算法 | LCG 线性同余算法 | Python Random 模块
117 0
|
21天前
|
Python
Python random模块(获取随机数)常用方法和使用例子
`random`模块在Python中用于生成随机数。
19 0
|
26天前
|
资源调度 Python
|
6月前
|
Python
python之math,random模块的使用方法
python之math,random模块的使用方法
|
7月前
|
数据挖掘 测试技术 数据安全/隐私保护
软件测试|Python random模块,超乎想象的强大
软件测试|Python random模块,超乎想象的强大
66 0
|
3月前
|
Python
Python学习 -- Math模块和Random模块
Python学习 -- Math模块和Random模块
11 0
|
3月前
|
Python
Python小姿势 - import random
Python小姿势 - import random
|
9月前
|
Python
Python: random模块简单使用_速看
Python: random模块简单使用_速看
59 0