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. 网络编程(一):用C#下载网络文件的2种方法

    使用C#下载一个Internet上的文件主要是依靠HttpWebRequest/HttpWebResonse和WebClient.具体处理起来还有同步和异步两种方式,所以我们其实有四种组合. 1.使用 ...

  2. Android开发-Android Studio使用问题解决

    回头一看,很久没来更新了,归其原因,还是懒癌发作,倒是生活作息规律了,几乎每天都在11点前休息.今天趁着培训,使用android studio,发现几个坑: 1.android studio每次都提示 ...

  3. 从头开始学算法--NUM operation in MIX

    从前往后,按照课本顺序刚刚看到MIX这部分.NUM是一个转换操作符,可以把字符编码转换为数字.它把registerA & registerX的值转换为数字并赋值给registerA.转换过程是 ...

  4. centos7使用传统网卡名

    http://serverfault.com/questions/692897/centos-7-disable-predictable-network-interface-names-during- ...

  5. 转:ServletContext,ActionContext,ServletActionContext

    ServletContext ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 javax.servlet.ServletCont ...

  6. promise实例小球运动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. TransactionScope类的使用

    如果在C#中使用TransactionScope类(分布式事务),则须注意如下事项:1.在项目中引用using System.Transactions命名空间(先要在添加net组件的引用); 2.具体 ...

  8. Xcode7中你一定要知道的炸裂调试神技

    转自:http://www.cocoachina.com/ios/20151020/13794.html Xcode7中苹果为我们增加了两个重要的debug相关功能.了解之后觉得非常实用,介绍给大家. ...

  9. MySQL中存储过程+事件的使用方法

    一.背景 将界面操作日志存储在MySQL数据库中的operationlog表中,如果该表不能自动备份,表中的数据会越来越多,影响速度.可以定期将表中数据备份到另外一个表中来解决. 二.解决方案 1.使 ...

  10. [Tomcat 源码分析系列] (一) : Tomcat 启动脚本-startup.bat

    概述 我们通常使用 Tomcat 中的 startup.bat 来启动 Tomcat. 但是这其中干了一些什么事呢? 大家都知道一个 Java 程序需要启动的话, 肯定需要 main 方法, 那么这个 ...