U e5dp@sddlmZddlmZmZmZ m Z m Z ddlmZmZmZmZddlmZddlmZmZddlmZ m!Z"ddl#m#Z$ddlZ%zdd l&m'Z&Wn e(k rdd l)m'Z&YnXd d d d ddddddddddddddddddd d!gZ*d"ed#ed$Z+d$e Z,ed%Z-d&ed'Z.d(Z/d)e/ Z0ddl1Z1Gd*d d e1j2Z2Gd+d!d!e2Z3d,d-Z4d4d/d0Z5e2Z6e6j7Z7e6j8Z8e6j9Z9e6j:Z:e6j;Z;e6jZ>e6j?Z?e6j@Z@e6jAZAe6jBZBe6jCZCe6jDZDe6jEZEe6jFZFe6jGZGe6jHZHe6jIZIe6jJZJe6jKZKe6jLZLeMe%d1re%jNe6j7d2eOd3kre5dS)5)warn)logexppieceil)sqrtacoscossin)urandom)SetSequence) accumulaterepeat)bisectN)sha512Randomseedrandomuniformrandintchoicesample randrangeshuffle normalvariatelognormvariate expovariatevonmisesvariate gammavariate triangulargauss betavariate paretovariateweibullvariategetstatesetstate getrandbitschoices SystemRandomg@@?@5cs eZdZdZd>ddZddZd?fdd Zfd d Zfd d ZddZ ddZ ddZ dde fddZ ddZddZe de>fddZeZddZd@dd Zd!d"ZdAddd#d$d%Zd&d'ZdBd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Z d}d|jkrqFd|jkr.|j|_qFd|jkr|j|_qFqdS)N _randbelowr(r)__mro____dict___randbelow_with_getrandbitsr:_randbelow_without_getrandbits)clskwargscr7r7r8__init_subclass__gs    zRandom.__init_subclass__r1cs|dkrt|ttfrt|tr*|dn|}|rBt|dd>nd}tt|D]}d||Ad@}qP|t|N}|dkr~dn|}|d krt|tttfrt|tr|}|t | 7}t |d }t |d|_dS) Nzlatin-1riCBlr1big) isinstancestrbytesdecodeordmaplen bytearrayencode_sha512Zdigestint from_bytessuperrr4)r5aversionr6rA __class__r7r8r{s    z Random.seedcs|jt|jfSr3)VERSIONrTr&r4r5rWr7r8r&szRandom.getstatec s|d}|dkr*|\}}|_t|nt|dkr|\}}|_ztdd|D}Wn(tk r|}z t|W5d}~XYnXt|ntd||jfdS)Nrr2r1css|]}|dVqdS)lNr7).0r6r7r7r8 sz"Random.setstate..z?state with version %s passed to Random.setstate() of version %s)r4rTr'tuple ValueError TypeErrorrY)r5staterVZ internalstaterrWr7r8r's  zRandom.setstatecCs|Sr3)r&rZr7r7r8 __getstate__szRandom.__getstate__cCs||dSr3)r')r5r`r7r7r8 __setstate__szRandom.__setstate__cCs|jd|fS)Nr7)rXr&rZr7r7r8 __reduce__szRandom.__reduce__rCc Cs||}||krtd|dkr:|dkr2||Std||}||krRtd||}|dkrx|dkrx|||S|dkrtd|||f||}||krtd|dkr||d|} n"|dkr||d|} ntd| dkrtd|||| S) Nz!non-integer arg 1 for randrange()rzempty range for randrange()z non-integer stop for randrange()rCz(empty range for randrange() (%d, %d, %d)z non-integer step for randrange()zzero step for randrange())r^r:) r5startstopstep_intZistartZistopwidthZistepnr7r7r8rs4  zRandom.randrangecCs|||dSNrC)rr5rUbr7r7r8rszRandom.randintcCs,|j}|}||}||kr(||}q|Sr3)r( bit_length)r5rir(krr7r7r8r=s  z"Random._randbelow_with_getrandbitscCsn|j}||kr$td|||S|dkr4td||}|||}|}||kr^|}qN||||S)NzUnderlying random() generator does not supply enough bits to choose from a population range this large. To remove the range limitation, add a getrandbits() method.rzBoundary cannot be zero)r_warnr^)r5rirRmaxsizerZremlimitror7r7r8r>s z%Random._randbelow_without_getrandbitscCs:z|t|}Wntk r0tddYnX||S)Nz$Cannot choose from an empty sequence)r:rNr^ IndexError)r5seqir7r7r8rs z Random.choicecCs|dkrN|j}ttdt|D]*}||d}||||||<||<q nHt}ttdt|D]0}|||d}||||||<||<qddSrj)r:reversedrangerNrR)r5r6r randbelowrujrgr7r7r8r%s  zRandom.shufflec Cst|trt|}t|ts$td|j}t|}d|krF|ksPntddg|}d}|dkr|dtt |dd7}||krt |}t |D]0}|||} || ||<|||d|| <qnHt } | j } t |D]2}||} | | kr||} q| | || ||<q|S) Nz>Population must be a sequence or set. For dicts, use list(d).rz,Sample larger than population or is negativer+r2rC)rH_Setr] _Sequencer_r:rNr^_ceil_loglistrwsetadd) r5 populationrnrxriresultZsetsizeZpoolruryZselectedZ selected_addr7r7r8r;s6)        z Random.sample) cum_weightsrncs|jtdkrV|dkrHtd7fddtd|DStt|n|dk rftdtkrztdtdddfddtd|DS) Ncsg|]}qSr7r7r[ru)rgrirrr7r8 sz"Random.choices..z2Cannot specify both weights and cumulative weightsz3The number of weights does not match the populationrErCcs$g|]}dqS)rr7r)rrhirrtotalr7r8rs) rrNrR_repeatr _accumulater_r^_bisect)r5rZweightsrrnr7)rgrrrrirrrr8r)s$  zRandom.choicescCs||||Sr3rrkr7r7r8rszRandom.uniformrr.cCs||}z |dkrdn||||}Wntk r@|YSX||krdd|}d|}||}}|||t||S)N?r.)rZeroDivisionError_sqrt)r5ZlowZhighmodeurAr7r7r8r!s    zRandom.triangularcCsP|j}|}d|}t|d|}||d}|t| krqDq|||S)Nr.rr-)r NV_MAGICCONSTr)r5musigmaru1u2zZzzr7r7r8rs   zRandom.normalvariatecCst|||Sr3)_expr)r5rrr7r7r8rszRandom.lognormvariatecCstd| |SNr.)rr)r5Zlambdr7r7r8rszRandom.expovariatecCs|j}|dkrt|Sd|}|td||}|}tt|}|||}|} | d||ks| d|t|kr4qq4d|} | |d| |} |} | dkr|t| t} n|t| t} | S)Ngư>rr.)rTWOPIr_cos_pir_acos)r5rZkapparsrorrdrqfZu3Zthetar7r7r8rs$   $zRandom.vonmisesvariatecCs~|dks|dkrtd|j}|dkrtd|d}|t}||}|}d|kr`dksdqFqFd|}t|d||} |t| } |||} ||| | } | td| dks| t| krF| |SqFn|dkrtd| |S|} t|t}|| }|dkr$|d|} nt||| } |}|dkr^|| |dkrpqrq|t| krqrq| |SdS)Nrz*gammavariate: alpha and beta must be > 0.0r.r,gHz>gP?r/)r^rrLOG4rr SG_MAGICCONST_e)r5alphabetarZainvZbbbZcccrrvr6rrorrlpr7r7r8r #s@        zRandom.gammavariatecCs`|j}|j}d|_|dkrT|t}tdtd|}t||}t|||_|||S)Ngr.)rr4rrrr_sin)r5rrrrZx2piZg2radr7r7r8r"hs  z Random.gausscCs0||d}|dkrdS||||dSdS)Nr.rr)r )r5rryr7r7r8r#s zRandom.betavariatecCsd|}d|d|Srr)r5rrr7r7r8r$s zRandom.paretovariatecCs"d|}|t| d|Sr)rr)r5rrrr7r7r8r%s zRandom.weibullvariate)N)Nr1)N)N)rr.N)#__name__ __module__ __qualname__rYr9rBrr&r'rarbrcrRrrr=BPFr>r:rrrr)rr!rrrrr r"r#r$r% __classcell__r7r7rWr8rNs:   ,  G  0E5 c@s4eZdZddZddZddZddZeZZd S) r*cCsttddd?tS)NrDrGr2)rRrS_urandom RECIP_BPFrZr7r7r8rszSystemRandom.randomcCs<|dkrtd|dd}tt|d}||d|?S)Nrz(number of bits must be greater than zerorDrG)r^rRrSr)r5rnZnumbytesr6r7r7r8r(s  zSystemRandom.getrandbitscOsdSr3r7r5argskwdsr7r7r8rszSystemRandom.seedcOs tddS)Nz*System entropy source does not have state.)NotImplementedErrorrr7r7r8_notimplementedszSystemRandom._notimplementedN) rrrrr(rrr&r'r7r7r7r8r*s cCsddl}t|d|jd}d}d}d}|}t|D]4} ||} || 7}|| | }t| |}t| |}q6|} tt| |dddd ||} t||| | } td | | ||fdS) Nrtimesrg _Bg _r2zsec, )endz"avg %g, stddev %g, min %g, max %g ) timeprintr perf_counterrwminmaxroundr)rifuncrrrZsqsumZsmallestZlargestZt0rur6t1ZavgZstddevr7r7r8_test_generators(     rcCst|tdt|tdt|tdt|tdt|tdt|tdt|tdt|tdt|tdt|tdt|td t|td t|td t|tdt|td t|td dS)Nr7)rr.)g{Gz?r.)皙?r.)rr,)rr.)g?r.)r.r.)r,r.)g4@r.)gi@r.)@r)rr.gUUUUUU?) rrrrrr r"r#r!)Nr7r7r8_tests                rfork)Zafter_in_child__main__)r)PwarningsrrpZmathrrrrrrrrrr~rrr rr rr rosr r_collections_abcr r|rr} itertoolsrrrrrr_osrQr ImportErrorZhashlib__all__rrrrrrZ_randomrr*rrZ_instrrrr!rrrrrr)rrrrr r"r#r$r%r&r'r(hasattrregister_at_forkrr7r7r7r8(s     {