[原創]Matlab 生成隨機數


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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM