【注】:洗牌等知识点未发表,可查看文末相关链接。

生成随机矩阵

用numpy包实现

生成随机整数矩阵

numpy.random.randint(low[,high,size]) 值范围位于半区间[low,high)中

python 矩阵 赋值 python对矩阵进行随机赋值_浮点数

其他函数

  • np.random.rand(size) 返回[0, 1)之间的随机浮点数矩阵。size为int型。
  • np.random.randn(size)返回符合正态分布的矩阵。size为int型。

生成随机数

用random包

  • 生成随机整数 random.randint[low,high]
  • 选取0到100间的偶数 random.randrange(0, 101, 2)
  • 随机浮点数
  • random.random() 生成(0,1)之间的浮点数
  • random.uniform(low,high)
  • 随机字符 random.choice(‘abcdefg&#%^*f’)
  • 多个字符中选取特定数量的字符 random.sample(‘abcdefghij’,3) 。随机提取三个字符。
  • 随机提取字符串 random.choice ( [‘apple’, ‘pear’, ‘peach’, ‘orange’, ‘lemon’] )