B l6mm K@sddlmZddlmZmZddlm Z m Z m ZmZmZddlmZmZmZmZddlmZddlmZ m!Z"ddl#m$Z%ddl&Z'ddl(Z)ddlZ*d d d d d ddddddddddddddddddd gZ+d!e d"ed#Z,d#eZ-e d$Z.d%e d&Z/d'Z0d(e0 Z1ddl2Z2Gd)d d e2j3Z3Gd*d d e3Z4d+d,Z5d3d.d/Z6e3Z7e7j8Z8e7j9Z9e7j:Z:e7j;Z;e7jZ>e7j?Z?e7j@Z@e7jAZAe7jBZBe7jCZCe7jDZDe7jEZEe7jFZFe7jGZGe7jHZHe7jIZIe7jJZJe7jKZKe7jLZLe7jMZMeNe*d0re*jOe7j8d1ePd2kre6dS)4)warn) MethodTypeBuiltinMethodType)logexppieceil)sqrtacoscossin)urandom)SetSequence)sha512NRandomseedrandomuniformrandintchoicesample randrangeshuffle normalvariatelognormvariate expovariatevonmisesvariate gammavariate triangulargauss betavariate paretovariateweibullvariategetstatesetstate getrandbitschoices SystemRandomgg@g@g?g@5cseZdZdZd:ddZd;fdd Zfdd Zfd d Zd d ZddZ ddZ dde fddZ ddZ e de>eeefddZddZdd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Z d8d9Z!Z"S)?rNcCs||d|_dS)N)r gauss_next)selfxr1)/opt/alt/python37/lib/python3.7/random.py__init__Xs zRandom.__init__r,cs|dkrt|ttfrt|tr*|dn|}|rBt|dd>nd}x"tt|D]}d||Ad@}qRW|t|N}|dkrdn|}|d krt|tttfrt|tr|}|t | 7}t |d }t |d|_dS) Nzlatin-1riCBlr,big) isinstancestrbytesdecodeordmaplen bytearrayencode_sha512Zdigestint from_bytessuperrr.)r/aversionr0c) __class__r1r2ras    z Random.seedcs|jt|jfS)N)VERSIONrEr%r.)r/)rIr1r2r%szRandom.getstatec s|d}|dkr*|\}}|_t|nt|dkr|\}}|_ytdd|D}Wn(tk r|}z t|Wdd}~XYnXt|ntd||jfdS)Nrr-r,css|]}|dVqdS)lNr1).0r0r1r1r2 sz"Random.setstate..z?state with version %s passed to Random.setstate() of version %s)r.rEr&tuple ValueError TypeErrorrJ)r/staterGZ internalstater)rIr1r2r&s  zRandom.setstatecCs|S)N)r%)r/r1r1r2 __getstate__szRandom.__getstate__cCs||dS)N)r&)r/rPr1r1r2 __setstate__szRandom.__setstate__cCs|jd|fS)Nr1)rIr%)r/r1r1r2 __reduce__szRandom.__reduce__r4c 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()r4z'empty range for randrange() (%d,%d, %d)z non-integer step for randrange()zzero step for randrange())rN _randbelow) r/startstopstep_intZistartZistopwidthZistepnr1r1r2rs4  zRandom.randrangecCs|||dS)Nr4)r)r/rFbr1r1r2rszRandom.randintc Cs|j}|j}|||ks$|||krN|} || } x| |krH|| } q6W| S||krltd|||S|dkr|td||} || |} |} x| | kr|} qW|| ||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)rr' bit_length_warnrN) r/rZrCmaxsizetypeZMethodZ BuiltinMethodrr'krZremlimitr1r1r2rTs&     zRandom._randbelowcCs:y|t|}Wntk r0tddYnX||S)Nz$Cannot choose from an empty sequence)rTr?rN IndexError)r/seqir1r1r2rs z Random.choicecCs|dkrR|j}xttdt|D]*}||d}||||||<||<q"WnLt}xFttdt|D]0}|||d}||||||<||<qjWdS)Nr4)rTreversedranger?rC)r/r0r randbelowrejrXr1r1r2rs   zRandom.shufflec Cs&t|trt|}t|ts$td|j}t|}d|krF|ksPntddg|}d}|dkr|dtt |dd7}||krt |}xt |D]0}|||} || ||<|||d|| <qWnRt } | j } xDt |D]8}||} x| | kr||} qW| | || ||<qW|S) Nz>Population must be a sequence or set. For dicts, use list(d).rz,Sample larger than population or is negativer*r-r4)r9_SetrM _SequencerOrTr?rN_ceil_loglistrgsetadd) r/ populationr`rhrZresultZsetsizeZpoolreriZselectedZ selected_addr1r1r2rs6       z Random.sample) cum_weightsr`cs|jdkrN|dkr>ttfddt|DStt|n|dk r^tdttkrvtdt j dtdfddt|DS)Ncsg|]}qSr1r1)rKre)rXrsrtotalr1r2 dsz"Random.choices..z2Cannot specify both weights and cumulative weightsz3The number of weights does not match the populationr6r4cs$g|]}dqS)rr1)rKre)bisectruhirsrrvr1r2rwms) rrCr?rgrp _itertools accumulaterOrN_bisectrx)r/rsZweightsrur`r1)rXrxruryrsrrvr2r(Xs  zRandom.choicescCs||||S)N)r)r/rFr[r1r1r2rtszRandom.uniform?cCsx|}y |dkrdn||||}Wntk r<|SX||kr`d|}d|}||}}|||t||S)Ng?g?)rZeroDivisionError_sqrt)r/ZlowZhighmodeurHr1r1r2r zs   zRandom.triangularcCsT|j}x@|}d|}t|d|}||d}|t| krPqW|||S)Ng?g?g@)r NV_MAGICCONSTro)r/musigmaru1u2zZzzr1r1r2rs   zRandom.normalvariatecCst|||S)N)_expr)r/rrr1r1r2rszRandom.lognormvariatecCstd| |S)Ng?)ror)r/Zlambdr1r1r2rszRandom.expovariatecCs|j}|dkrt|Sd|}|td||}xN|}tt|}|||}|} | d||ks~| d|t|kr6Pq6Wd|} | |d| |} |} | dkr|t| t} n|t| t} | S)Ngư>g?g?)rTWOPIr_cos_pir_acos)r/rZkapparsrarrdrqfZu3Zthetar1r1r2rs&   $zRandom.vonmisesvariatecCs|dks|dkrtd|j}|dkrtd|d}|t}||}x|}d|krbdksfqHqHd|}t|d||} |t| } |||} ||| | } | td| dks| t| krH| |SqHWn|dkr |} x| dkr|} qWt|  |Sx|} t|t}|| }|dkr@|d|} nt||| } |}|dkrx|| |dkrPn|t| krPqW| |SdS)Ngz*gammavariate: alpha and beta must be > 0.0g?g@gHz>gP?g@)rNrrLOG4ror SG_MAGICCONST_e)r/alphabetarZainvZbbbZcccrrvr0rrarr[pr1r1r2rsJ          zRandom.gammavariatecCs`|j}|j}d|_|dkrT|t}tdtd|}t||}t|||_|||S)Ngg?)rr.rrror_sin)r/rrrrZx2piZg2radr1r1r2r!@s  z Random.gausscCs0||d}|dkrdS||||dSdS)Ng?rg)r)r/rryr1r1r2r"us zRandom.betavariatecCsd|}d|d|S)Ng?)r)r/rrr1r1r2r#s zRandom.paretovariatecCs"d|}|t| d|S)Ng?)rro)r/rrrr1r1r2r$s zRandom.weibullvariate)N)Nr,)N)N)r}r~N)#__name__ __module__ __qualname__rJr3rr%r&rQrRrSrCrrBPFr_ _MethodType_BuiltinMethodTyperTrrrr(rr rrrrrr!r"r#r$ __classcell__r1r1)rIr2rHs6   ,  :  0H5 c@s4eZdZddZddZddZddZeZZd S) r)cCsttddd?tS)Nr5r8r-)rCrD_urandom RECIP_BPF)r/r1r1r2rszSystemRandom.randomcCsP|dkrtd|t|kr$td|dd}tt|d}||d|?S)Nrz(number of bits must be greater than zeroz#number of bits should be an integerr5r8)rNrCrOrDr)r/r`Znumbytesr0r1r1r2r's  zSystemRandom.getrandbitscOsdS)Nr1)r/argskwdsr1r1r2rszSystemRandom.seedcOs tddS)Nz*System entropy source does not have state.)NotImplementedError)r/rrr1r1r2_notimplementedszSystemRandom._notimplementedN) rrrrr'rrr%r&r1r1r1r2r)s  cCsddl}t|d|jd}d}d}d}|}x@t|D]4} ||} || 7}|| | }t| |}t| |}q8W|} tt| |dddd ||} t||| | } td | | ||fdS) Nrtimesgg _Bg _r-zsec, )endz"avg %g, stddev %g, min %g, max %g ) timeprintrZ perf_counterrgminmaxroundr)rZfuncrrrvZsqsumZsmallestZlargestZt0rer0Zt1ZavgZstddevr1r1r2_test_generators&  rcCst|tdt|tdt|tdt|tdt|tdt|tdt|tdt|tdt|tdt|tdt|td t|td t|td t|tdt|td t|td dS)Nr1)gg?)g{Gz?g?)g?g?)g?g@)g?g?)g?g?)g?g?)g@g?)g4@g?)gi@g?)g@g@)gg?gUUUUUU?) rrrrrrr!r"r )Nr1r1r2_tests                rfork)Zafter_in_child__main__)r)Qwarningsrr]typesrrrrZmathrrorrrrrrr rnr rr rr rr rosrr_collections_abcrrlrrmZhashlibrrB itertoolsrzrxr|_os__all__rrrrrrZ_randomrr)rrZ_instrrrr rrrrrr(rrrrrr!r"r#r$r%r&r'hasattrregister_at_forkrr1r1r1r2(sr     Y!