matlab指定区间随机数产生

本文介绍了如何在MATLAB中使用rand和randi函数生成指定区间的随机数。rand函数用于产生[0, 1)之间的浮点数,通过乘以范围差加区间起点可得到指定区间;randi函数则生成指定范围内的整数,包括边界值。同时,文章展示了如何控制随机数生成的种子,确保结果可复现。" 103027160,9065501,CIFS文件系统与Samba服务器配置详解,"['Linux', '网络服务', '文件共享', '系统管理', 'SMB']

1. 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.
 
    Examples:
 
       Example 1: Generate values from the uniform distribution on the
       interval [a, b].
          r = a + (b-a).*rand(100,1);
 
       Example 2: Use the RANDI function, instead of rand, to generate
       integer values from the uniform distribution on the set 1:100.
          r = randi(100,1,5);
 
       Example 3: Reset the random number generator used by rand, RANDI, and
       RANDN to its default startup settings, so that rand produces the same
       random numbers as if you restarted MATLAB.
          rng('default')
          rand(1,5)
 
       Example 4: Save the settings for the random number generator used by
       rand, RANDI, and RANDN, generate 5 values from rand, restore the
       settings, and repeat those values.
          s = rng
          u1 = rand(1,5)
          rng(s);
          u2 = rand(1,5) % contains exactly the same values as u1
 
       Example 5: Reinitialize the random number generator used by rand,
       RANDI, and RANDN with a seed based on the current time.  rand will
       return different values each time you do this.  NOTE: It is usually
       not necessary to do this more than once per MATLAB session.
          rng('shuffle');
          rand(1,5)


2. 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.
 
    Examples:
 
       Example 1: Generate integer values from the uniform distribution on
       the set 1:10.
          r = randi(10,100,1);
 
       Example 2: Generate an integer array of integer values drawn uniformly
       from 1:10.
          r = randi(10,100,1,'uint32');
 
       Example 3: Generate integer values drawn uniformly from -10:10.
          r = randi([-10 10],100,1);
 
       Example 4: Reset the random number generator used by RAND, randi, and
       RANDN to its default startup settings, so that randi produces the same
       random numbers as if you restarted MATLAB.
          rng('default');
          randi(10,1,5)
 
       Example 5: Save the settings for the random number generator used by
       RAND, randi, and RANDN, generate 5 values from randi, restore the
       settings, and repeat those values.
          s = rng
          i1 = randi(10,1,5)
          rng(s);
          i2 = randi(10,1,5) % i2 contains exactly the same values as i1
 
       Example 6: Reinitialize the random number generator used by RAND,
       randi, and RANDN with a seed based on the current time.  randi will
       return different values each time you do this.  NOTE: It is usually
       not necessary to do this more than once per MATLAB session.
          rng('shuffle');
          randi(10,1,5)


For example: If I want to generate 24*1 array from [5,24], the code is:

r = randi([5 14],24,1); 

It is the same as: r=randi(10,24,1)+4




r =


    13
    13
    14
     5
    13
    11
    14
    10
     9
    13
     7
     9
    14
    10
    13
    12
    10
     7
    11
     5
    11
    11
    12
    13


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值