|||
函数:
rand: 均匀分布
randi:均匀分布的伪随机整数
randn: 正态分布
randperm:随机排列
rng:控制随机数生成
1. rand
r = rand(3) %% 3x3 0到1之间随机数 r = 0.9649 0.9572 0.1419 0.1576 0.4854 0.4218 0.9706 0.8003 0.9157
要想获得单行/列随机数:
r = -3 + 6*rand(1,3) %% 1行3列 -3 到 3之间的随机数
2. randi
randi(10) %% 一个 1 到 10 之间的随机数 randi(10, 3) %% 3x3 1 到 10 之间的随机数 数组 randi(10,1,3) %% 1x3 1 到 10 之间的随机数 数组 ans = 4 10 1 randi([-5,5],3,1) %% 3x1 -5 到 5 之间的随机数 数组
3. randn
randn(3) %% 3x3 正态分布随机数
4. randperm
randperm(6) %% 1x6 1 到6 随机行向量 randperm(6,3) %% 1x3 1 到6 随机行向量
5. rng 主要是一个种子 (seed)的概念
s = rng; %% 保存随机数生成的设置 x = rand(1,5) x = 0.8407 0.2543 0.8143 0.2435 0.9293 rng(s); %% 调用原来的随机数生成设置 y = rand(1,5) %% 产生的随机数和 x 一样 y = 0.8407 0.2543 0.8143 0.2435 0.9293
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-27 08:19
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社