lpswpeng的个人博客分享 http://blog.sciencenet.cn/u/lpswpeng

博文

Matlab -- Mathematics 1

已有 2101 次阅读 2019-7-20 22:53 |个人分类:Matlab|系统分类:科研笔记| matlab, 自学

Mathematics -- 随机数生成

函数:

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




https://blog.sciencenet.cn/blog-3417594-1190418.html


收藏 IP: 141.5.13.*| 热度|

1 张学文

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-20 22:48

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部