python随机数

记录随机数的用法。

package

import random

randint()

函数原型

获取某个范围内的随机数,这个区间是闭区间:

randint(self, a, b) method of Random instance
    Return random integer in range [a, b], including both end points.

示例

Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> print random.randint(1,2)
1
>>> print random.randint(1,2)
2
>>> print random.randint(100,200)
116
>>> import sys
>>> print random.randint(int(sys.maxint), sys.maxint)
2147483647
>>> print sys.maxint
2147483647
>>> print random.randint(int(sys.maxint / 10), sys.maxint)
1622687942
>>>

random doc

可以用pydoc random查看所有doc。以下摘录部分内容。

betavariate(self, alpha, beta) method of Random instance
    Beta distribution.

    Conditions on the parameters are alpha > 0 and beta > 0.
    Returned values range between 0 and 1.

choice(self, seq) method of Random instance
    Choose a random element from a non-empty sequence.

expovariate(self, lambd) method of Random instance
    Exponential distribution.

    lambd is 1.0 divided by the desired mean.  It should be
    nonzero.  (The parameter would be called "lambda", but that is
    a reserved word in Python.)  Returned values range from 0 to
    positive infinity if lambd is positive, and from negative
    infinity to 0 if lambd is negative.

gammavariate(self, alpha, beta) method of Random instance
    Gamma distribution.  Not the gamma function!

    Conditions on the parameters are alpha > 0 and beta > 0.

    The probability distribution function is:

                x ** (alpha - 1) * math.exp(-x / beta)
      pdf(x) =  --------------------------------------
                  math.gamma(alpha) * beta ** alpha

gauss(self, mu, sigma) method of Random instance
    Gaussian distribution.

    mu is the mean, and sigma is the standard deviation.  This is
    slightly faster than the normalvariate() function.

    Not thread-safe without a lock around calls.

getrandbits(...)
    getrandbits(k) -> x.  Generates a long int with k random bits.

getstate(self) method of Random instance
    Return internal state; can be passed to setstate() later.

jumpahead(self, n) method of Random instance
    Change the internal state to one that is likely far away
    from the current state.  This method will not be in Py3.x,
    so it is better to simply reseed.

lognormvariate(self, mu, sigma) method of Random instance
    Log normal distribution.

    If you take the natural logarithm of this distribution, you'll get a
    normal distribution with mean mu and standard deviation sigma.
    mu can have any value, and sigma must be greater than zero.

normalvariate(self, mu, sigma) method of Random instance
    Normal distribution.

    mu is the mean, and sigma is the standard deviation.

paretovariate(self, alpha) method of Random instance
    Pareto distribution.  alpha is the shape parameter.

randint(self, a, b) method of Random instance
    Return random integer in range [a, b], including both end points.

random(...)
    random() -> x in the interval [0, 1).

randrange(self, start, stop=None, step=1, _int=<type 'int'>, _maxwidth=9007199254740992L) method of Random instance
    Choose a random item from range(start, stop[, step]).

    This fixes the problem with randint() which includes the
    endpoint; in Python this is usually not what you want.

sample(self, population, k) method of Random instance
    Chooses k unique random elements from a population sequence.

    Returns a new list containing elements from the population while
    leaving the original population unchanged.  The resulting list is
    in selection order so that all sub-slices will also be valid random
    samples.  This allows raffle winners (the sample) to be partitioned
    into grand prize and second place winners (the subslices).

    Members of the population need not be hashable or unique.  If the
    population contains repeats, then each occurrence is a possible
    selection in the sample.

    To choose a sample in a range of integers, use xrange as an argument.
    This is especially fast and space efficient for sampling from a
    large population:   sample(xrange(10000000), 60)

seed(self, a=None) method of Random instance
    Initialize internal state from hashable object.

    None or no argument seeds from current time or from an operating
    system specific randomness source if available.

    If a is not None or an int or long, hash(a) is used instead.

setstate(self, state) method of Random instance
    Restore internal state from object returned by getstate().

shuffle(self, x, random=None) method of Random instance
    x, random=random.random -> shuffle list x in place; return None.

    Optional arg random is a 0-argument function returning a random
    float in [0.0, 1.0); by default, the standard random.random.

triangular(self, low=0.0, high=1.0, mode=None) method of Random instance
    Triangular distribution.

    Continuous distribution bounded by given lower and upper limits,
    and having a given mode value in-between.

    http://en.wikipedia.org/wiki/Triangular_distribution

uniform(self, a, b) method of Random instance
    Get a random number in the range [a, b) or [a, b] depending on rounding.

vonmisesvariate(self, mu, kappa) method of Random instance
    Circular data distribution.

    mu is the mean angle, expressed in radians between 0 and 2*pi, and
    kappa is the concentration parameter, which must be greater than or
    equal to zero.  If kappa is equal to zero, this distribution reduces
    to a uniform random angle over the range 0 to 2*pi.

weibullvariate(self, alpha, beta) method of Random instance
    Weibull distribution.

    alpha is the scale parameter and beta is the shape parameter.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值