Python 3 Random 모듈 코드 상세 설명

11044 단어 python3random 모듈
묘사 하 다.
random()방법 은 무 작위 로 생 성 된 실 수 를 되 돌려 줍 니 다.[0,1)범위 내 에 있 습 니 다.

import random
help(random)
FUNCTIONS
  betavariate(alpha, beta) method of Random instance #        
    Beta distribution. # β  
    
    Conditions on the parameters are alpha > 0 and beta > 0. #       0 alpha  beta  
    Returned values range between 0 and 1. #     0 -1     ,        !
    a = random.betavariate(999999, 99999999999999999) #        :9.995974671839104e-12        :1.0006927848540756e-11
        (Beta Distribution)                             ,                 。
         ,    ,  Β  ,       (0,1)          。
  
  choice(seq) method of Random instance
    Choose a random element from a non-empty sequence. #                   ,       
    Python   6       ,    、  、   、Unicode   、buffer   xrange  。
  
  choices(population, weights=None, *, cum_weights=None, k=1) method of Random instance #        
    Return a k sized list of population elements chosen with replacement. #   chosen with replacement(                    ,                )        k          
    
    If the relative weights or cumulative weights are not specified, #               ,
    the selections are made with equal probability.          #         。
     population     k   (   )。          。
      :
    print(random.choices(['red', 'black', 'green'], [18, 18, 2], k=6) ) #  18      red,18      black,2      green,   6 
    
    trial = lambda: random.choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5 # H    0.6,T    0.4
    print(sum(trial() for i in range(10000)) / 10000)

    trial2 = lambda : 2500 <= sorted(random.choices(range(10000), k=5))[2] < 7500
    print(sum(trial2() for i in range(10000)) / 10000 )

    from statistics import mean
    data = 1, 2, 4, 4, 10
    means = sorted(mean(random.choices(data, k=5)) for i in range(20)) # mean    
    print(f'The sample mean of {mean(data):.1f} has a 90% confidence ' 
   f'interval from {means[1]:.1f} to {means[-2]:.1f}') #    f   
    #               :
    # The sample mean of 4.2 has a 90% confidence interval from 2.4 to 6.6
    # The sample mean of 4.2 has a 90% confidence interval from 2.6 to 7.2
    # The sample mean of 4.2 has a 90% confidence interval from 2.0 to 7.0

  expovariate(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.
    λ(lambd) 1      ,     。(       lambda,   python   )
            0         (   λ   ),          0    ,  (   λ   )
  
  gammavariate(alpha, beta) method of Random instance #        
    Gamma distribution. Not the gamma function! #     .  gamma  
    
    Conditions on the parameters are alpha > 0 and beta > 0. #               0
    
    The probability distribution function is: #        :
    
          x ** (alpha - 1) * math.exp(-x / beta)
     pdf(x) = --------------------------------------
           math.gamma(alpha) * beta ** alpha
  
  gauss(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.
    # mu   ,sigma   ,     normalvariate()         
    #       ,     。
    #        ,mu sigma,
  
  getrandbits(...) method of Random instance #        
    getrandbits(k) -> x. Generates an int with k random bits. #        k    (    )。             (    ),        。
  
  getstate() method of Random instance #        
    Return internal state; can be passed to setstate() later. #                 .         setstate()        .
  
  lognormvariate(mu, sigma) method of Random instance #        
    Log normal distribution.
          (logarithmic 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.
    #                 ,         mu,    sigma     。
    # mu       ,sigma      0
  
  normalvariate(mu, sigma) method of Random instance #        
    Normal distribution. #     ,           
    
    mu is the mean, and sigma is the standard deviation. # mu   ,sigma   
  
  paretovariate(alpha) method of Random instance #        
    Pareto distribution. alpha is the shape parameter. #      ;  alpha     
    #      (Pareto distribution)             ・      ,                      ,
    #           ,          。        20%     80%         ,      ・   
    #            (80/20  ),                。

  
  randint(a, b) method of Random instance #        
    Return random integer in range [a, b], including both end points. #   a b         ,   a b
    #         , a, b      ,      ,a        b  。
  
  random(...) method of Random instance #        
    random() -> x in the interval [0, 1). #    ,    0-1      ,   0,     1
  
  randrange(start, stop=None, step=1, _int=<class 'int'>) method of Random instance #        
    Choose a random item from range(start, stop[, step]). #               
    
    This fixes the problem with randint() which includes the #        randint()              
    endpoint; in Python this is usually not what you want.  # randint()                         
           ,start   stop  
  
  sample(population, k) method of Random instance #        
    Chooses k unique random elements from a population sequence or set. #    population        k     
    
    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                     。
    population contains repeats, then each occurrence is a possible   #   population         ,                  
    selection in the sample.
    
    To choose a sample in a range of integers, use range as an argument #                 ,            。
    This is especially fast and space efficient for sampling from a   #                          
    large population:  sample(range(10000000), 60)           #   :sample(range(10000000), 60)
  
  seed(a=None, version=2) method of Random instance #        
    Initialize internal state from hashable object.          #               
    
    None or no argument seeds from current time or from an operating #    None        , seeds              
    system specific randomness source if available.          #               (      )
    
    If *a* is an int, all bits are used.               #     a   ,   bits      
    
    For version 2 (the default), all of the bits are used if *a* is a str, #  version   2(    ),    a        ,bytes bytearray,    bits      
    bytes, or bytearray. For version 1 (provided for reproducing random  #  version   1 (      python          )
    sequences from older versions of Python), the algorithm for str and   #    str,bytes             。
    bytes generates a narrower range of seeds.
  
  setstate(state) method of Random instance #        
    Restore internal state from object returned by getstate(). #   getstate()        。
  
  shuffle(x, random=None) method of Random instance #        
    Shuffle list x in place, and return None. #     x   None x       
    
    Optional argument random is a 0-argument function returning a #               0-1        
    random float in [0.0, 1.0); if it is the default None, the   #           None,     random.random    
    standard random.random will be used.
                   ,                    ,                    。
  
  triangular(low=0.0, high=1.0, mode=None) method of Random instance #        
    Triangular distribution. #     (triangular distribution),             
    
    Continuous distribution bounded by given lower and upper limits, #          low、   high、                 。
    and having a given mode value in-between.
    
    http://en.wikipedia.org/wiki/Triangular_distribution
  
  uniform(a, b) method of Random instance #        
    Get a random number in the range [a, b) or [a, b] depending on rounding. #  a b        ,      a,b     b   
    a b       ,         ,         b     math.ceil
                 
  
  vonmisesvariate(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.
    # mu       ,     0-2*pi  ,kappa       ,        0。
    #   kappa  0,         ,  κ     ,        ,      0-2*pi  

  
  weibullvariate(alpha, beta) method of Random instance
    Weibull distribution. #      
    
    alpha is the scale parameter and beta is the shape parameter. # λ>0     (scale parameter),k>0     (shape parameter)
    #    alpha     ,beta     
         (Weibull distribution),            ,                。
         :            ,                      。                       ,
                     。
총결산
이상 은 Python 3 Random 모듈 코드 에 대한 상세 한 내용 입 니 다.여러분 께 도움 이 되 기 를 바 랍 니 다.관심 이 있 는 친 구 는 본 사이트 의 다른 관련 주 제 를 계속 참고 할 수 있 습 니 다.부족 한 점 이 있 으 면 댓 글로 지적 해 주 십시오.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기