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

本文实例讲述了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

运行效果图如下:

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生成随机数组的方法小结的更多相关文章

  1. numpy生成随机数组

    python想要生成随机数的话用使用random库很方便,不过如果想生成随机数组的话,还是用numpy更好更强大一点. 生成长度为10,在[0,1)之间平均分布的随机数组: rarray=numpy. ...

  2. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  3. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  4. python生成随机日期字符串

    python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 # ...

  5. Python生成随机字符串

    利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, cho ...

  6. python生成随机整数

    python生成随机不重复的整数,用random中的sample index = random.sample(range(0,10),10) 上面是生成不重复的10个从1~10的整数 python生成 ...

  7. [三卷天书]记一个asp.net生成两个日期范围内生成随机时间的方法

    想网上找个生成随机天数的方法找不到,后面只得自己写了,贴给大家方便使用 思路:算两个日期的相差天数,然后在0到相差天数的范围内生成随机数,再用结束时间的天数部分减去这个随机数,代码: /// < ...

  8. PHP 生成随机数组

    /** 生成指定个数,以及最小最大值随机数组(包括最大值) * @parem $min 随机数组最小值 * @parem $max 随机数组最大值 * @parem $num 随机数组个数,默认max ...

  9. Java中生成随机字符的方法总结

    package learnExercise; public class RandomCharacter { public static char getRandomCharacter(char ch1 ...

随机推荐

  1. RT-Thread--简介

    RT-Thread 概述 RT-Thread,全称是 Real Time-Thread,它是一个嵌入式实时多线程操作系统,基本属性之一是支持多任务,允许多个任务同时运行,但并不是真正的同时运行,而是宏 ...

  2. Spark2 jar存档

    spark.yarn.archive需要手动将spark应用依赖jar上传到hdfs,该属性可以避免每一次运行spark应用时都重复打zip包上传到hdfs. 官网http://spark.apach ...

  3. Android笔记-Activity相关+内存泄漏+Fragment+service

    看了下,上次学习android还是17年的事情,,,,两年过去了我现在终于来搞android了... 官网有一段基础描述: https://developer.android.google.cn/gu ...

  4. 【H5】 经纬度位置获取navigator.geolocation.getCurrentPosition

    navigator.geolocation.getCurrentPosition(function(){})经度 : coords.longitude 纬度 : coords.latitude 准确度 ...

  5. python_tkinter弹出对话框1

    tkinter提供了三个模块,可以创建弹出对话窗口:(使用必须单独导入模块) 1.messagebox 消息对话框 示例:askokcancel import tkinter # 导入消息对话框子模块 ...

  6. LINQ to DataSet,对离线数据的Linq支持、AsEnumeable()

    一.DataTable的扩展方法: 1.DataTable转Linq:AsEnumerable 方法 返回IEnumerable<T>对象,其中泛型参数T是DataRow. 此对象可用在 ...

  7. 关于pageHelper无法查到总数踩到的坑

    问题代码 PageHelper.startPage(pageNum,pageSize); List<pojoVo> pojoVo=robotService.getPageList(); P ...

  8. DbVisualizer用JDBC连接SQL Server

    1.安装驱动,解压后得到jar文件 Drivers 4.1 and 4.0 for SQL Serve http://www.microsoft.com/en-us/download/confirma ...

  9. Security Study

    1.WebGoat http://www.owasp.org.cn/ 学习Web应用漏洞最好的教程----WebGoat http://blog.csdn.net/bill_lee_sh_cn/art ...

  10. 数据库学习之三--Select查询及运算符

    一.SELECT语句:用于从表中选取数据:语法如下: 1. 列查询: SELECT 列名称1,  列名称2 FROM 表名称: 2. 查询所有数据: SELECT * FROM 表名称: 3. 使用A ...