博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《python》---random函数
阅读量:4230 次
发布时间:2019-05-26

本文共 3565 字,大约阅读时间需要 11 分钟。

感谢博主:mishidemudong

地址:

第一部分 函数介绍

研究排序问题的时候常常需要生成随机数组来验证自己排序算法的正确性和性能,所以把Python生成随机数(组)的方法稍作总结,以备以后查看使用。

整数参数间的一个随机整数[low,hight)
1 random.randint(low,hight)
0-1之间随机浮点数左闭右开
2 random.random()
参数之间随机浮点数全闭合
3 random.uniform(val1,val2)
起点到终点按照间隔取得随机数
4 random.randrange(start, stop, step)

random.randint(low, hight)

>>> help(random.randint)Help on method randint in module random:randint(self, a, b) method of random.Random instanceReturn random integer in range [a, b], including both end points.

返回一个位于[low,hight]之间的整数。

该函数必须接受两个参数,这两个参数必须是整数(或者小数位是0的浮点数),
第一个整数参数必须小于等于第二个整数参数

>>> random.randint(1, 10)6>>> random.randint(1.0, 10.0)8

random.random()

>>> help(random.random)Help on built-in function random:random(...)    random() -> x in the interval [0, 1).

不接受参数,返回一个[0.0, 1.0)之间的浮点数

>>> random.random()0.16638892353491153

random.uniform(val1, val2)

>>> help(random.uniform)Help on method uniform in module random:uniform(self, a, b) method of random.Random instance    Get a random number in the range [a, b) or [a, b] depending on rounding.

接受两个数字参数,返回两个数字区间的一个浮点数,不要求val1小于等于val2.

>>> random.uniform(1,4)3.5630633737872053

random.randrange(start, stop, step)

>>> help(random.randrange)Help on method randrange in module random:randrange(self, start, stop=None, step=1, _int=
, _maxwidth=9007199254740992L) method of random.Random instance Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want.

返回以start开始,stop结束,step为步长的列表中的随机整数,同样,三个参数均为整数(或者小数位为0),

若start大于stop时 ,setp必须为负数
step不能是小数

>>> random.randrange(1, 100, 2)17>>> random.randrange(100, 1, -2)28

第二部分 利用函数产生随机数组

import numpy as np

用于生成[0.0, 1.0)之间的随机浮点数。
np.random.rand(n)
用于生成[0.0, 1.0)之间的随机浮点数,但是具有正态分布特性。
np.random.randn(n)
返回随机的整数,位于半开区间 [low, high),size表示返回的个数。
np.random.randint(low[, high, size])
返回随机的整数,位于闭合区间 [low, high],size表示返回的个数。
np.random_integers(low[, high, size])
将序列中的元素进行随机顺序排列
np.random.shuffle(arr)
对arange(n)的arr进行位置随机排序,对比shuffle,直接产生arr
np.random.permutation(n)

np.random.rand(n)

用于生成[0.0, 1.0)之间的随机浮点数。

当没有参数时,返回一个随机浮点数。
当有一个参数时,返回该参数长度大小的一维随机浮点数数组,参数是整数型。

>>> np.random.rand(3)array([ 0.46905771,  0.11792115,  0.38069103])>>> type(np.random.rand())
>>> type(np.random.rand(3))

np.random.randn(n)

该函数返回具有标准正态一个样本。

>>> np.random.randn()-0.26784706346125065>>> np.random.randn(5)array([-1.2499142 , -0.06773846, -0.44777215, -1.55111136,  0.14573448])>>> type(np.random.rand())
>>> type(np.random.rand(3))

np.random.randint(low[, high, size])

返回随机的整数,位于半开区间 [low, high),且第一个参数默认是0可以省略。建议写出size参数关键字

>>> np.random.randint(2, 10)           #因为没有写出size7>>> type(np.random.randint(0,2))
>>> np.random.randint(2, size=10)array([0, 1, 0, 1, 0, 0, 0, 1, 1, 1])>>> np.random.randint(0,2, 10) # 所以不建议如此写array([1, 0, 1, 1, 0, 1, 0, 0, 0, 0])>>> np.random.randint(0,2, size=10)array([0, 0, 0, 1, 0, 0, 0, 0, 1, 1])# 初始化二维数组>>> np.random.randint(5, size=(2, 4))array([[4, 4, 4, 4], [3, 0, 2, 0]])

np.random_integers(low[, high, size])

返回随机的整数,位于闭区间 [low, high]。

np.random.random_integers(0,2, size=10)

array([0, 1, 2, 0, 1, 1, 0, 0, 0, 1])
np.random.random_integers(5, size=(3,2))
array([[5, 4],
[3, 3],
[4, 5]])

np.random.shuffle(x)

类似洗牌,打乱顺序

>>> arr = np.arange(10)>>> arrarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> np.random.shuffle(arr)>>> arrarray([4, 2, 8, 1, 9, 7, 0, 6, 5, 3])

np.random.permutation(n)

返回一个随机排列,相当于上面产生list的函数+shuffle函数的共同效果

>>> np.random.permutation(10)array([8, 5, 6, 7, 4, 0, 3, 1, 9, 2])

转载地址:http://roiqi.baihongyu.com/

你可能感兴趣的文章
How to Cheat at Securing a Wireless Network
查看>>
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
查看>>
Smarty Php Template Programming And Applications
查看>>
Web Site Measurement Hacks
查看>>
The Best Damn Windows Server 2003 Book Period
查看>>
Cleaning Windows XP For Dummies
查看>>
The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
查看>>
Python in a Nutshell
查看>>
Microsoft Visual C++ .NET Professional Projects
查看>>
Excel 2007 Advanced Report Development
查看>>
Security Metrics: Replacing Fear, Uncertainty, and Doubt
查看>>
Accelerating Process Improvement Using Agile Techniques
查看>>
The New Language of Business: SOA & Web 2.0
查看>>
Programming Mobile Devices: An Introduction for Practitioners
查看>>
Designing for Networked Communications: Strategies and Development
查看>>
Building a Monitoring Infrastructure with Nagios
查看>>
Illustrated C# 2005
查看>>
Pro ASP.NET 2.0 E-Commerce in C# 2005
查看>>
Thinking Animation: Bridging the Gap Between 2D and CG
查看>>
Ajax in Practice
查看>>