numpy中的random函數


1:rand

    rand(d0, d1, ..., dn)
    Random values in a given shape.
    Create an array of the given shape and populate it with
    random samples from a uniform distribution

    over ``[0, 1)``.


    數字區間:[0,1)

    分布:均勻分布

    形狀:[d0,d1,...,dn]

from numpy import random
print(random.rand(3,4))
'''result
[[0.77647254 0.87714719 0.55351719 0.31369393]
 [0.38578822 0.30977858 0.31366171 0.26879944]
 [0.22720179 0.26118622 0.08420711 0.70508725]]
'''

2:randint

    randint(low, high=None, size=None, dtype='l')
    Return random integers from `low` (inclusive) to `high` (exclusive).
    Return random integers from the "discrete uniform" distribution of
    the specified dtype in the "half-open" interval [`low`, `high`). If

    `high` is None (the default), then results are from [0, `low`).


    數字區間:[low,high)

    分布:離散均勻分布

    形狀:size

from numpy import random
print(random.randint(1,10, size=(2,3)))
'''result
[[3 1 6]
 [9 1 7]]
 '''

3:randn

    randn(d0, d1, ..., dn)
    Return a sample (or samples) from the "standard normal" distribution.
    If positive, int_like or int-convertible arguments are provided,
    `randn` generates an array of shape ``(d0, d1, ..., dn)``, filled
    with random floats sampled from a univariate "normal" (Gaussian)
    distribution of mean 0 and variance 1 (if any of the :math:`d_i` are
    floats, they are first converted to integers by truncation). A single
    float randomly sampled from the distribution is returned if no
    argument is provided.
    This is a convenience function.  If you want an interface that takes a
    tuple as the first argument, use `numpy.random.standard_normal` instead.


    數字區間:(負無窮,正無窮)

    分布:標准正態分布

    形狀:[d0,d1,...,dn]

from numpy import random
print(random.randn(3,2))
'''result
[[ 0.0456255   0.64865066]
 [-0.40588788  0.0428462 ]
 [ 0.46260185 -0.05147188]]
'''

4: ranf = random = sample = random_sample

random_sample(size=None)

Return random floats in the half-open interval [0.0, 1.0).

Results are from the "continuous uniform" distribution over the
stated interval.  To sample :math:`Unif[a, b), b > a` multiply
the output of `random_sample` by `(b-a)` and add `a`::

  (b - a) * random_sample() + a


    數字區間:[0,1)

    分布:連續均勻分布

    形狀:size

    注意:ranf、random、sample、random_sample 都是使用的random_sample方法

             要想得到a到b之間的隨機數,使用  (b - a) * random_sample() + a

from numpy import random
print(random.random())    #result 0.7679449887445754
print(random.random(size=(2,2)))
'''result
[[0.05636011 0.46029369]
 [0.26693099 0.34289541]]
'''

5:normal

normal(loc=0.0, scale=1.0, size=None)

Draw random samples from a normal (Gaussian) distribution.

The probability density function of the normal distribution, first
derived by De Moivre and 200 years later by both Gauss and Laplace
independently [2]_, is often called the bell curve because of
its characteristic shape (see the example below).

The normal distributions occurs often in nature. For example, it
describes the commonly occurring distribution of samples influenced
by a large number of tiny, random disturbances, each with its own
unique distribution [2]_.


 

    數字區間:(負無窮,正無窮)

    分布:均值為loc,標准差為scale的正態分布

    形狀:size

from numpy import random
print(random.normal(0.0, 0.01, [2, 3]))
'''result
[[-0.01117429  0.00404763  0.01438945]
 [ 0.00550622 -0.01674051 -0.00411558]]
'''

 


免責聲明!

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



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