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之用while来循环
Oct 02 Python
Python实现批量下载文件
May 17 Python
python django 实现验证码的功能实例代码
May 18 Python
python3+PyQt5实现自定义分数滑块部件
Apr 24 Python
Django 根据数据模型models创建数据表的实例
May 27 Python
浅谈PyQt5 的帮助文档查找方法,可以查看每个类的方法
Jun 25 Python
Django如何将URL映射到视图
Jul 29 Python
python文字转语音实现过程解析
Nov 12 Python
Python模块的制作方法实例分析
Dec 21 Python
python 获取字典键值对的实现
Nov 12 Python
jupyter 添加不同内核的操作
Feb 06 Python
Python+Matplotlib图像上指定坐标的位置添加文本标签与注释
Apr 11 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
极典R601SW收音机
2021/03/02 无线电
PHP简单实现HTTP和HTTPS跨域共享session解决办法
2015/05/27 PHP
Laravel中使用FormRequest进行表单验证方法及问题汇总
2016/06/19 PHP
PHP基于自增数据如何生成不重复的随机数示例
2017/05/19 PHP
Linux下 php7安装redis的方法
2018/11/01 PHP
借用Google的Javascript API Loader来加速你的网站
2009/01/28 Javascript
关于innerHTML后丢失动态绑定的EVENT问题解决方法
2013/05/19 Javascript
通过jquery 获取URL参数并进行转码
2014/08/18 Javascript
jquery实现个人中心导航菜单效果和美观都非常不错
2014/09/02 Javascript
javascript学习笔记(四)function函数部分
2014/09/30 Javascript
JavaScript静态类型检查工具FLOW简介
2015/01/06 Javascript
js判断日期时间有效性的方法
2015/10/24 Javascript
zTree插件下拉树使用入门教程
2016/04/11 Javascript
微信小程序switch开关选择器使用详解
2018/01/31 Javascript
基于vue-element组件实现音乐播放器功能
2018/05/06 Javascript
js replace 全局替换的操作方法
2018/06/12 Javascript
详解angular2如何手动点击特定元素上的点击事件
2018/10/16 Javascript
JS Array.from()将伪数组转换成数组的方法示例
2020/03/23 Javascript
[03:09]DOTA2亚洲邀请赛 LGD战队出场宣传片
2015/02/07 DOTA
python检测某个变量是否有定义的方法
2015/05/20 Python
Python匹配中文的正则表达式
2016/05/11 Python
Python编程实现控制cmd命令行显示颜色的方法示例
2017/08/14 Python
Python 3.7新功能之dataclass装饰器详解
2018/04/21 Python
使用pandas实现csv/excel sheet互相转换的方法
2018/12/10 Python
解决pytorch多GPU训练保存的模型,在单GPU环境下加载出错问题
2020/06/23 Python
python实现图片素描效果
2020/09/26 Python
python中添加模块导入路径的方法
2021/02/03 Python
C#里面如何判断一个Object是否是某种类型(如Boolean)?
2016/02/10 面试题
银行实习鉴定
2013/12/13 职场文书
蓝颜请假条
2014/04/11 职场文书
公司建议书怎么写
2014/05/15 职场文书
幼儿园教研工作总结2015
2015/05/12 职场文书
幸福来敲门观后感
2015/06/04 职场文书
湘江北去观后感
2015/06/15 职场文书
小学总务工作总结
2015/08/13 职场文书
Elasticsearch 批量操作
2022/04/19 Python