import random
a = random.sample(range(4), 2) #在range(4)区间随机生成4个不重复的数字
print(type(a),a)
#<class 'list'> [2, 0]
b = random.sample(range(4), 2)[0]
print(type(b),b)
#<class 'int'> 0
import numpy as np
a = np.random.random((2, 4)) #随机生成两行四列的浮点数矩阵
print(type(a),a)
#<class 'numpy.ndarray'> [[0.84059612 0.80616937 0.04167652 0.84283596]
[0.03478196 0.36639656 0.87453693 0.1008385 ]]
a = np.random.rand(3) #随机返回一个服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1),不包括1
b = np.random.rand(2,3) #随机返回一组服从“0~1”均匀分布的随机样本值。随机样本取值范围是[0,1),不包括1
print(type(a),a)
print(type(b),b)
#<class 'numpy.ndarray'> [0.50127127 0.55601353 0.20055031]
#<class 'numpy.ndarray'> [[0.72295991 0.61202242 0.78460299]
# [0.13239045 0.27219538 0.31517006]]
a = np.random.randn(3) #随机返回一个服从标准正态分布的随机样本值
b = np.random.randn(2,3) #随机返回一组服从标准正态分布的随机样本值。
print(type(a),a)
print(type(b),b)
#<class 'numpy.ndarray'> [-0.1747378 0.30162623 -0.90583185]
#<class 'numpy.ndarray'> [[-0.1862383 -0.99574306 1.02645022]
# [ 0.28366771 -1.26058523 -1.30896123]]
a = np.random.standard_normal((2,3)) ##随机返回一组服从标准正态分布的随机样本值。输入为元组
print(a)
#[[ 0.14565661 -1.04146553 -0.8953934 ]
#[-2.20026964 1.16004027 0.16351675]]
a = np.random.randint(2,8,[2,3],dtype='int32') #返回2行3列的数字,数据为大于等于2小于8的整数
print(a)
#[[3 5 4]
#[4 6 6]]
num = 0
while num < 4:
np.random.seed(3) #相同的seed()值,则每次生成的随即数都相同
print(np.random.random(2))
num += 1
#[0.5507979 0.70814782]
#[0.5507979 0.70814782]
#[0.5507979 0.70814782]
#[0.5507979 0.70814782]
a = np.arange(10)
np.random.shuffle(a) #随机打乱数据顺序
print(a)