Matlab中有着丰富的随机数生成函数以应用于不同的情景,我一般使用生成随机的1~N的整数,但是之前了解的只有rand函数,其生成主要为0~1之间的随机数,但是和所预想的有差异。在此进行进行了help指令,之后了解到了randi函数,并初步学会使用,在此做一个记录。

rand函数

rand函数是生产0~1的随机数,rand(N)为生产一个N行N列的随机数矩阵,rand(M,N)为生成一个M行N列的随机数矩阵。以下为一些示例。

>> rand(3)

ans =

    0.8147    0.9134    0.2785
    0.9058    0.6324    0.5469
    0.1270    0.0975    0.9575

>> rand(2, 3)

ans =

    0.9649    0.9706    0.4854
    0.1576    0.9572    0.8003

在help rand后,我们可以观察其解释说明。

>> help rand
 rand Uniformly distributed pseudorandom numbers.
    R = rand(N) returns an N-by-N matrix containing pseudorandom values drawn
    from the standard uniform distribution on the open interval(0,1).  rand(M,N)
    or rand([M,N]) returns an M-by-N matrix.  rand(M,N,P,...) or
    rand([M,N,P,...]) returns an M-by-N-by-P-by-... array.  rand returns a
    scalar.  rand(SIZE(A)) returns an array the same size as A.

    Note: The size inputs M, N, P, ... should be nonnegative integers.
    Negative integers are treated as 0.

    R = rand(..., 'double') or R = rand(..., 'single') returns an array of
    uniform values of the specified class.

    The sequence of numbers produced by rand is determined by the settings of
    the uniform random number generator that underlies rand, RANDI, and RANDN.
    Control that shared random number generator using RNG.

通过最后其提示的See also,我们可以观看其他和随机数有关的函数,看有没有合适的函数。

See also randi, randn, rng, RandStream, RandStream/rand,
         sprand, sprandn, randperm.

randi函数

产生1~NUM的随机整数,NUM可调整。其中NUM作为一个输入的参数。randi(MAX, N)产生一个最大值为MAX的N行N列的整数矩阵,randi(MAX, M, N)产生一个最大值为MAX的M行N列的整数矩阵。以下为一些示例。

>> randi(5, 6)

ans =

     1     1     5     3     1     1
     2     5     2     1     2     4
     1     5     1     1     2     4
     1     3     4     5     5     4
     2     3     2     5     1     3
     3     2     2     3     1     3

>> randi(5, 3, 9)

ans =

     2     4     4     5     3     3     4     5     5
     4     1     4     4     3     3     4     3     5
     1     2     1     3     2     5     2     2     3

在help randi后,我们可以观察其解释说明。

>> help randi
 randi Pseudorandom integers from a uniform discrete distribution.
    R = randi(IMAX,N) returns an N-by-N matrix containing pseudorandom
    integer values drawn from the discrete uniform distribution on 1:IMAX.
    randi(IMAX,M,N) or randi(IMAX,[M,N]) returns an M-by-N matrix.
    randi(IMAX,M,N,P,...) or randi(IMAX,[M,N,P,...]) returns an
    M-by-N-by-P-by-... array.  randi(IMAX) returns a scalar.
    randi(IMAX,SIZE(A)) returns an array the same size as A.

    R = randi([IMIN,IMAX],...) returns an array containing integer
    values drawn from the discrete uniform distribution on IMIN:IMAX.

    Note: The size inputs M, N, P, ... should be nonnegative integers.
    Negative integers are treated as 0.

    R = randi(..., CLASSNAME) returns an array of integer values of class
    CLASSNAME.

    The arrays returned by randi may contain repeated integer values.  This
    is sometimes referred to as sampling with replacement.  To get unique
    integer values, sometimes referred to as sampling without replacement,
    use RANDPERM.

    The sequence of numbers produced by randi is determined by the settings of
    the uniform random number generator that underlies RAND, RANDN, and randi.
    randi uses one uniform random value to create each integer random value.
    Control that shared random number generator using RNG.
 
 

[原创]Matlab生成随机数的更多相关文章

  1. 【转】利用matlab生成随机数函数

    原文地址:利用matlab生成随机数函数 rand(n):生成0到1之间的n阶随机数方阵  rand(m,n):生成0到1之间的m×n的随机数矩阵 (现成的函数) betarnd:贝塔分布的随机数生成 ...

  2. [原创]Matlab之GUI生成EXE文件

    近期因为项目需要,简化流程,写了一些Matlab程序,并配备上了GUI界面使其简单易用.然后问题来了,可移植性.使用Matlab生成EXE文件(可以封装很多的function),然后在一台安装有Mat ...

  3. Matlab生成Word--xdd

    摘自<MATLAB统计分析与应用:40个案例分析>(谢中华老师著)P452页function CreatWord %利用Matlab生成word filespec_user = [pwd ...

  4. MATLAB生成exe脱离matlab运行可执行程序

    https://blog.csdn.net/u013007900/article/details/53485204 侵权即删. ———————————————— 版权声明:本文为CSDN博主「小木匠_ ...

  5. matlab 生成.exe文件 转

    本文链接:https://blog.csdn.net/qq_20823641/article/details/51863737 如何将MATLAB程序编译成独立可执行的程序?如何将编译好的独立可执行程 ...

  6. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  7. DotNet生成随机数的一些方法

    在项目开发中,一般都会使用到“随机数”,但是在DotNet中的随机数并非真正的随机数,可在一些情况下生成重复的数字,现在总结一下在项目中生成随机数的方法. 1.随机布尔值: /// <summa ...

  8. Oracle中生成随机数的函数(转载)

    在Oracle中的DBMS_RANDOM程序包中封装了一些生成随机数和随机字符串的函数,其中常用的有以下两个: DBMS_RANDOM.VALUE函数 该函数用来产生一个随机数,有两种用法: 1. 产 ...

  9. [转载]Matlab生成Word报告

    最近在进行一批来料的检验测试,一个个手动填写报告存图片太慢了,就有了种想要使用Matlab在分析完后数据可以自动生成PDF报告的想法,于是就去网上搜索了相关的资料,发现Matlab中文论坛上有xiez ...

随机推荐

  1. OpenGL学习笔记1——第一个程序

    学习的参考书基本是按照GL编程指南,如果有消息机制概念,对于GLUT的理解是很自然的.下面就按照自己写的第一个程序详细解释一下GL,还是比较容易上手的. 程序实现的功能是,根据当前随即种子摇出来的结果 ...

  2. SQL Server performance tips

    Refer to: http://harriyott.com/2006/01/sql-server-performance-tips A colleague of mine has been look ...

  3. PHP左、右、内连接

    left join   :左连接,返回左表中所有的记录以及右表中连接字段相等的记录.right join :右连接,返回右表中所有的记录以及左表中连接字段相等的记录.inner join: 内连接,又 ...

  4. JS获取文本值

    var closeBtn=document.getElementById("close_btn"): closeBtn.innerHTML即是文本值,如果要的是数字parseInt ...

  5. 2016年Java服务器端开发面试总结

    因为一些个人原因,陆陆续续参加了一些校招补招.社招的笔试和面试(BAT),也包括一些国外公司(Spotify, Google, Ericsson),最后有幸被网易收入麾下,拿到了S.下面罗列一些我复习 ...

  6. C# 计算两个字符串的相似度

    我们在做数据系统的时候,经常会用到模糊搜索,但是,数据库提供的模糊搜索并不具备按照相关度进行排序的功能. 现在提供一个比较两个字符串相似度的方法. 通过计算出两个字符串的相似度,就可以通过Linq在内 ...

  7. java学习第15天(Linklist Vector)

    根据集合的分类(上一天有说),首先接触的是ArrayList但是和Collection一样,他没有什么特殊的功能,直接跳过,然后是Vector. 一   Vector A:有特有功能 a:添加 pub ...

  8. Spring概念

    1.控制反转IOC:即生成对象,相当于new的功能,用IOC有利于维护. 2.依赖注入DI:即给属性赋值,相当于JavaBean的setter方法. 3.面向切面方程AOP:使得各个切面和目标类完全松 ...

  9. 转:认识MyBean

    1. 初步体验 我们先看一个框架自带的例子,以增加感性认识.打开samples\singleDEMO示例项目.这个示例演示了在一个EXE程序内,使用插件的概念调用两个窗口.其中包括一个主窗体 ufrm ...

  10. html大牛属性

    <META http-equiv="Content-Type" content="text/htmll;charset=gb2312"> 字体 &l ...