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. 测不准的程序员(Heisenberg Developers)

    摘译自http://mikehadlow.blogspot.co.uk/2014/06/heisenberg-developers.html   ——“你无法在不改变他们状态的情况下观察一个开发者” ...

  2. GPUImage实现摄像头暂停

    GPUImage相信很多开发者都用过,其本身也是十分简单易用,因此对于GPUImage的基本用法不再赘述. 最近在使用GPUImage时要实现摄像时暂停和继续的功能,但GPUImage本身并没有提供相 ...

  3. QTP操作excel文档

    对于QTP操作excel的大前提是,保证组建服务里的DCOM配置里存在 microsoft excel application ,具体的查看方式是,在运行框中输入dcomcnfg,然后会打开组件服务的 ...

  4. hbm.xml 详解总结

    转自 http://blog.csdn.net/tuke_tuke/article/details/49717991 一.hibernate映射文件的作用: Hibernate映射文件是Hiberna ...

  5. 跨域访问CORS

    由于现代浏览器的同源策略,合理的跨域请求也变得至关重要. CORS(Cross-Origin Resource Sharing,跨域资源共享)定义了在必须访问跨域资源时,浏览器与服务器应该如何沟通.它 ...

  6. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  7. Lua学习笔记(一)-----C#和lua的交互

    一直以来对Lua热更新技术很感兴趣,在上周开始了对Lua的学习,主要学的是uLua. 直接上干货 准备工作: LuaInterface包括两个核心库一个是LuaInterface.dll,一个是Lua ...

  8. Maximo子表中增加附件功能

    附件功能的实现(详见ewell.webclient.beans.warranty.WarrantysDateBean ,ewell.webclient.beans.doclinks.custom.Ad ...

  9. javascript模式之模块模式

    使用模式来组织代码有很多优点:使代码的结构更清晰,逻辑性更强,更容易维护.还可以避免很多错误. 首先,在javascript主要分为两大类: 编程模式-- 一些专门为javascript语言开发出的最 ...

  10. teeChart 修改注册表实现无试用到期提示

    teechart在注册表的跟目录为HKEY_CLASSES_ROOT\CLSID,以下的子目录会根据版本的不同发生改变,但是最底级的目录是TypeLib(例如[HKEY_CLASSES_ROOT\CL ...