matlab rand 函数

本文详细解释了MATLAB中rand函数的工作原理及其使用方法。包括如何通过'state'和'seed'参数来初始化随机数生成器的状态,从而实现可重复的随机数序列生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们经常看到rand('state',10) ,rand('seed',10)  不知道什么意思。
关于rand 具体用法 可以输入help rand 
>> help rand
RAND   Uniformly distributed random numbers.
    RAND(N) is an N-by-N matrix with random entries, chosen from
    a uniform distribution on the interval (0.0,1.0).
    RAND(M,N) and RAND([M,N]) are M-by-N matrices with random entries.
    RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays.
    RAND with no arguments is a scalar whose value changes each time it
    is referenced.  RAND(SIZE(A)) is the same size as A.  
 
    RAND produces pseudo-random numbers.  The sequence of numbers
    generated is determined by the state of the generator.  Since MATLAB
    resets the state at start-up, the sequence of numbers generated will
    be the same unless the state is changed.
 
    S = RAND('state') is a 35-element vector containing the current state
    of the uniform generator.  RAND('state',S) resets the state to S.
    RAND('state',0) resets the generator to its initial state.
    RAND('state',J), for integer J, resets the generator to its J-th state.
    RAND('state',sum(100*clock)) resets it to a different state each time.
 
    This generator can generate all the floating point numbers in the 
    closed interval [2^(-53), 1-2^(-53)].  Theoretically, it can generate
    over 2^1492 values before repeating itself.
 
    MATLAB Version 4.x used random number generators with a single seed.
    RAND('seed',0) and RAND('seed',J) cause the MATLAB 4 generator to be used.
    RAND('seed') returns the current seed of the MATLAB 4 uniform generator.
    RAND('state',J) and RAND('state',S) cause the MATLAB 5 generator to be used.

 看了这一大堆英文可能还不如一个例子讲得明白,看下面程序:

>> rand('state',10)
>> a=rand

a =

    0.4493

>> a

a =

    0.4493

>> a=rand

a =

    0.2088

>> rand('state',10) %reset the state to the 10-th state.
>> a=rand

a =

    0.4493 %%从这可以看到又跟开始一样了。所以每次设置rand('state',10)后获得的随机数相同。

>> rand('seed',10)
>> a=rand

a =

    0.1291

>> a

a =

    0.1291

>> rand

ans =

    0.6048

>> rand('seed',10)
>> a=rand

a =

    0.1291  %%又是0.1291


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值