Python生成随机数组的方法小结


Posted in Python onApril 15, 2017

本文实例讲述了Python生成随机数组的方法。分享给大家供大家参考,具体如下:

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

一、使用random模块生成随机数组

python的random模块中有一些生成随机数字的方法,例如random.randint, random.random, random.uniform, random.randrange,这些函数大同小异,均是在返回指定范围内的一个整数或浮点数,下边简单解释一下这几个函数。

1、random.randint(low, hight) -> 返回一个位于[low,hight]之间的整数

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

>>> import random
>>> random.randint(1,10)
5
>>> random.randint(1.0, 10.0)
5

2、random.random() -> 不接受参数,返回一个[0.0, 1.0)之间的浮点数

>>> random.random()
0.9983625479554628

3、random.uniform(val1, val2) -> 接受两个数字参数,返回两个数字区间的一个浮点数,不要求val1小于等于val2

>>> random.uniform(1,5.0)
2.917249424176132
>>> random.uniform(9.9, 2)
3.4288029275359024

*4、random.randrange(start, stop, step) -> 返回以start开始,stop结束,step为步长的列表中的随机整数,同样,三个参数均为整数(或者小数位为0),若start大于stop时 ,setp必须为负数.step不能是0.*

>>> random.randrange(1, 100, 2) #返回[1,100]之间的奇数
95
>>> random.randrange(100, 1, -2) #返回[100,1]之间的偶数
46

运行效果图如下:

Python生成随机数组的方法小结

5、生成随机数组

下边我们用random.randint来生成一个随机数组

import random
def random_int_list(start, stop, length):
  start, stop = (int(start), int(stop)) if start <= stop else (int(stop), int(start))
  length = int(abs(length)) if length else 0
  random_list = []
  for i in range(length):
    random_list.append(random.randint(start, stop))
  return random_list

接下来我们就可以用这个函数来生成一个随机的整数序列了

>>> random_int_list(1,100,10)
[54, 13, 6, 89, 87, 39, 60, 2, 63, 61]

二、使用numpy.random模块来生成随机数组

1、np.random.rand 用于生成[0.0, 1.0)之间的随机浮点数, 当没有参数时,返回一个随机浮点数,当有一个参数时,返回该参数长度大小的一维随机浮点数数组,参数建议是整数型,因为未来版本的numpy可能不支持非整形参数。

import numpy as np
>>> np.random.rand(10)
array([ 0.56911206, 0.99777291, 0.18943144, 0.19387287, 0.75090637,
    0.18692814, 0.69804514, 0.48808425, 0.79440667, 0.66959075])

当然该函数还可以用于生成多维数组,这里不做详述。

2、np.random.randn该函数返回一个样本,具有标准正态分布。

>>> np.random.randn(10)
array([-1.6765704 , 0.66361856, 0.04029481, 1.19965741, -0.57514593,
    -0.79603968, 1.52261545, -2.17401814, 0.86671727, -1.17945975])

3、np.random.randint(low[, high, size]) 返回随机的整数,位于半开区间 [low, high)。

>>> np.random.randint(10,size=10)
array([4, 1, 4, 3, 8, 2, 8, 5, 8, 9])

4、random_integers(low[, high, size]) 返回随机的整数,位于闭区间 [low, high]。

>>> np.random.random_integers(5)
4

5、np.random.shuffle(x) 类似洗牌,打乱顺序;np.random.permutation(x)返回一个随机排列

>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8]
>>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
Python 相关文章推荐
基于Python的身份证号码自动生成程序
Aug 15 Python
python实现在pickling的时候压缩的方法
Sep 25 Python
Python生成随机MAC地址
Mar 10 Python
用pickle存储Python的原生对象方法
Apr 28 Python
Python中使用支持向量机(SVM)算法
Dec 26 Python
Python实现读取txt文件并转换为excel的方法示例
May 17 Python
解决pandas无法在pycharm中使用plot()方法显示图像的问题
May 24 Python
python 实现对数据集的归一化的方法(0-1之间)
Jul 17 Python
关于Python作用域自学总结
Jun 10 Python
详解opencv中画圆circle函数和椭圆ellipse函数
Dec 27 Python
python通过移动端访问查看电脑界面
Jan 06 Python
详解在OpenCV中如何使用图像像素
Mar 03 Python
Python中文分词工具之结巴分词用法实例总结【经典案例】
Apr 15 #Python
Python结巴中文分词工具使用过程中遇到的问题及解决方法
Apr 15 #Python
Python编程实现生成特定范围内不重复多个随机数的2种方法
Apr 14 #Python
Python编程判断一个正整数是否为素数的方法
Apr 14 #Python
python编程实现归并排序
Apr 14 #Python
python实现折半查找和归并排序算法
Apr 14 #Python
Python+Wordpress制作小说站
Apr 14 #Python
You might like
php+html5使用FormData对象提交表单及上传图片的方法
2015/02/11 PHP
jquery $.ajax入门应用二
2008/11/19 Javascript
JavaScript 学习小结(适合新手参考)
2009/07/30 Javascript
jQuery 页面 Mask实现代码
2010/01/09 Javascript
JavaScript中常用的运算符小结
2012/01/18 Javascript
jquery实现metro效果示例代码
2013/09/06 Javascript
使用AOP改善javascript代码
2015/05/01 Javascript
AngularJS中的过滤器filter用法完全解析
2016/04/22 Javascript
Angular中实现树形结构视图实例代码
2017/05/05 Javascript
vue-cli+webpack在生成的项目中使用bootstrap实例代码
2017/05/26 Javascript
vue的状态管理模式vuex
2017/11/30 Javascript
详解Vue This$Store总结
2018/12/17 Javascript
微信公众号H5支付接口调用方法
2019/01/10 Javascript
vue-cli中vue本地实现跨域调试接口
2019/01/16 Javascript
javascript防抖函数debounce详解
2019/06/11 Javascript
微信小程序左滑删除实现代码实例
2019/09/16 Javascript
详解Vue的watch中的immediate与watch是什么意思
2019/12/30 Javascript
python实现的希尔排序算法实例
2015/07/01 Python
python魔法方法-属性转换和类的表示详解
2016/07/22 Python
Python列表删除的三种方法代码分享
2017/10/31 Python
Python中类的初始化特殊方法
2017/12/01 Python
对python 通过ssh访问数据库的实例详解
2019/02/19 Python
Python使用matplotlib实现交换式图形显示功能示例
2019/09/06 Python
Python中__repr__和__str__区别详解
2019/11/07 Python
django前端页面下拉选择框默认值设置方式
2020/08/09 Python
通过代码实例解析Pytest运行流程
2020/08/20 Python
PyQt5多线程防卡死和多窗口用法的实现
2020/09/15 Python
详解如何在css中引入自定义字体(font-face)
2018/05/17 HTML / CSS
绘画设计学生的个人自我评价
2013/09/20 职场文书
运动会广播稿100字
2014/01/11 职场文书
劳动者解除劳动合同通知书
2015/04/16 职场文书
爸爸的三轮车观后感
2015/06/16 职场文书
2015小学新教师个人工作总结
2015/10/14 职场文书
导游词之云南丽江-泸沽湖
2019/09/26 职场文书
Python基础学习之奇异的GUI对话框
2021/05/27 Python
苹果的回收机器人可以通过拆解iPhone获取大量的金和铜并外公布了环境保护最新进展
2022/04/21 数码科技