ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

各式随机数的生成Python

2021-09-06 21:32:49  阅读:154  来源: 互联网

标签:返回 各式 randint Python random 随机数 np import 10


random

1. random.sample(x,y):  从序列x中,随机选择y个不重复的元素,返回list列表

import random
#从[A,B)间随机生成N个不重复的数,结果以列表返回
resultList=random.sample(range(A,B),N)

2. random.randint(low, hight): 从[low,hight]之间的返回一个整数

import random
x=random.randint(1,10)

3. random.random(): 从[0.0, 1.0)之间的返回一个浮点数

import random
x=random.random()

numpy.random

1. np.random.rand(): 当没有参数时,返回[0.0, 1.0)之间的一个随机浮点数;当有一个参数时,返回该参数长度大小的一维随机浮点数数组

import numpy as np

x1=np.random.rand()
x2=np.random.rand(10)

2. np.random.randn():  与上述一致,但返回一个样本具有标准正态分布

3. np.random.randint(low[, high, size]):一个参数时,返回小于low的随机整数;两个参数时,返回位于半开区间 [low, high)的随机整数;三个参数时,返回大小为size的位于[low, high)之间的数组,size可以指定数组形式

import numpy as np

x1=np.random.randint(10)
#  Out[]: 9
x2=np.random.randint(10,12,2)
#  Out[]: array([10, 10])
x3=np.random.randint(1,12,(2,3))
#  Out[]: 
#array([[6, 6, 5],
#       [6, 7, 3]])
x4=np.random.randint(1,12,(2,3,4))

4. np.random.permutation(high): 如果传入单个整数,返回[0,high)的所有整数的随机排列结果;如果传入数组,返回扰乱结果

import numpy as np

x1=np.random.permutation(10)
# Out[]: array([3, 4, 0, 1, 8, 2, 9, 7, 5, 6])

x2=np.random.permutation([2,5,5,4])
# Out[]: array([5, 4, 5, 2])

5. np.random.shuffle(x): 传入数组或列表,自身打乱顺序,不返回结果

import numpy as np

x=np.array([1,2,3])   #  x:[1,2,3]
 
x1=np.random.shuffle(x)  # x:[3,1,2]  x1:None

标签:返回,各式,randint,Python,random,随机数,np,import,10
来源: https://blog.csdn.net/weijie_home/article/details/120127456

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有