Python3 Random module code details, python3random

Source: Internet
Author: User

Python3 Random module code details, python3random

Description

The random () method returns a random real number within the range of [0, 1.

Import randomhelp (random) FUNCTIONS betavariate (alpha, beta) method of Random instance # method of random instance Beta distribution. # beta distribution Conditions on the parameters are alpha> 0 and beta> 0. # The alpha and beta parameters Returned values range between 0 and 1 must be input. # Return a value between 0 and 1. You are random! A = random. betavariate (999999,999 9999999999999999) # first execution result: 9.995974671839104e-12 second execution result: 1.0006927848540756e-11 Beta Distribution) it is a density function that serves as the bounded prior distribution of bernuoli distribution and binary distribution. It is used in machine learning and mathematical statistics. In probability theory, beta distribution, also known as Gini distribution, refers to a group of continuous probability distributions defined in the (0, 1) interval. Choice (seq) method of Random instance Choose a random element from a non-empty sequence. # randomly retrieve an element from a sequence not empty. The parameter must not be empty. Python contains six built-in sequences, including list, tuples, strings, Unicode strings, buffer objects, and xrange objects. 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 (that is, each extraction opportunity is the same or weighted, and the previous selection will not affect the probability of subsequent selection) if the relative weights or cumulative weights are not specified, # If the relative weight or cumulative weight is not specified, the selections are made with equal probability. # The selected probability is equal. K elements (repeated) are randomly extracted from population ). Two parameters cannot exist at the same time. Example: print (random. choices (['red', 'black', 'green'], [18, 18, 2], k = 6) # Use the Probability Weight of 18 to get red, the Probability Weight of 18 is black, and the Probability Weight of 2 is green. A total of 6 trial = lambda: random. choices ('ht ', cum_weights = (0.60, 1.00), k = 7 ). count ('H')> = 5 # The probability of H is 0.6, and the probability of T is 0.4 print (sum (trial () for I in range (10000)/10000) trial = lambda: 2500 <= sorted (random. choices (range (10000), k = 5) [2] <7500 print (sum (trial' () for I in range (10000)/1000 0) from statistics import mean data = 1, 2, 4, 4, 10 means = sorted (mean (random. choices (data, k = 5) for I in range (20) # mean is The average print (f'the sample mean of {mean (data ):. 1f} has a 90% confidence 'F' interval from {means [1]:. 1f} to {means [-2]:. 1f} ') # f usage # the preceding three execution results are as follows: # 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 inter Val 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 # method of Random instance Exponential distribution. # exponential distribution lambd is 1.0 divided by the desired mean. it shoshould be nonzero. (The parameter wocould 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) is 1 divided by the required number, and it cannot be zero. (The parameter should be called lambda, but it is a reserved word in python) This function returns a random number ranging from 0 to positive infinity (If λ is positive), or returns a random number ranging from negative infinity to 0, if (If λ is negative) gammavariate (alpha, beta) method of Random instance # method of Random instance Gamma distribution. not the gamma function! # Gamma distribution. it is not the gamma function Conditions on the parameters are alpha> 0 and beta> 0. # The condition of The parameter is that both parameters must be greater than 0 The probability distribution function is: # 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 # method of Random instance Gaussian distribution. # Gaussian distribution, also known as normal distribution or normal distribution mu is the mean, and sigma is the standa Rd deviation. this is slightly faster than the normalvariate () function. not thread-safe without a lock around CILS. # mu is expected, and sigma is variance. This function is a little faster than the normalvariate () function. # No thread is called, and the thread is not secure. # There are two required parameters: mu and sigma, getrandbits (...) method of Random instance # method of Random instance getrandbits (k)-> x. generates an int with k random bits. # Return k random bits (binary number) in integer form ). This function is particularly useful for real random transactions (such as encryption. Getstate () method of Random instance # Random instance method Return internal state; can be passed to setstate () later. # Return the object that captures the internal status of the current generator. this object can be used by the setstate function () to save the current state. lognormvariate (mu, sigma) method of Random instance # method of Random instance Log normal distribution. logarithmic normal distribution refers to the log distribution of a random variable following the normal distribution. The log normal distribution is very close to the normal distribution in the short term. However, in the long run, the log normal distribution has more upward distribution values. 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. # If you take the natural logarithm of the distribution parameter, you will get a normal distribution with the mean number mu and the standard deviation sigma. # The mu parameter is of any value. The sigma parameter must be greater than 0 normalvariate (mu, sigma) method of Random instance # Normal distribution of the Random instance method. # normal distribution, also known as normal distribution and Gaussian distribution mu is the mean, and sigma is the standard deviation. # mu is expected, and sigma is variance paretovariate (alpha) method of Random instance # method of Random instances, namely, the maximum number of Random instances. alpha is the shape parameter. #; the alpha parameter is the shape parameter # The Name Of The Pareto distribution is Italian economist verfredo parreto, is the distribution of power laws found from a large number of real-world phenomena ,# This distribution is also known as the Bradford distribution outside of economics. He is well-known for his observation of 20% of the population of Italy's property of 80%. Later, he was summarized by Joseph Zhu and # others as the "six-digit law ), later, it was further summarized as the concept of the "Pareto distribution. Randint (a, B) method of Random instance # Return random integer in range [a, B], including both end points. # Return a random integer between a and B. In this case, a and B # Must be input. both a and B must be integers, which can be negative integers, a must be smaller than or equal to B. Random (...) method of Random instance # method of random instance Random ()-> x in the interval [0, 1 ). # if there is no parameter, a random number between 0 and 1 is generated, which can be 0, but 1 randrange (start, stop = None, step = 1, _ int = <class 'int'>) method of Random instance # method of random instance Choose a Random item from range (start, stop [, step]). # randomly select a parameter This fixes the problem with randint () which has des the # This function fixes the endpoint where randint () can obtain the number on the right of the range; in Python this is u Sually not what you want. # randint () can obtain numbers in the right range. Generally, the parameters that many users do not want to see must be integers, And the start parameter must be smaller than the stop parameter sample (population, k) method of Random instance # method of random instance Chooses k unique Random elements from a population sequence or set. # retrieve k random elements from a population sequence or set Returns a new list containing elements from the population while # Return a list containing the preceding elements. the original sequence or set will not change. Leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random # the returned list is arranged in The selected order. In this way, all subslices will also be valid random samples. Samples. this allows raffle winners (the sample) to be partitioned # in This way, the lucky ones in the sample can be sorted in the order of first place and second place (in the returned List) into grand prize and second place winners (the subslices ). members of the population need not be hashable or unique. if the # population sequence members do not need to be hashable or not duplicated. Population contains repeats, then each occurrence is a possible # If the population sequence contains duplicate members, each selection will be possible selection in the sample. to choose a sample in a range of integers, use range as an argument # To select a sample within an integer range, use the range value as a parameter. This is especially fast and space efficient for sampling from a # This will be very fast and save memory large population: sample (range (10000000), 60) when sampling from large data) # example: sample (range (10000000), 60) seed (a = None, version = 2) method of Random instance # method of Random instance Initialize internal state from hashable object. # initialize the internal state None or no argument seeds from current time or from an operating by using a hash object # When the parameter is None or no parameter, then seeds selects this value based on the current time. System specific randomness source if available. # If * a * is an int, all bits are used. # if parameter a is an integer, all bits will be used For version 2 (the default), all of the bits are used if * a * is a str, # When the version parameter is 2 (the default parameter), if parameter a is a string type, bytes or bytearray, all bits will be used bytes, or bytearray. for version 1 (provided for reproducing random # When the version parameter is 1 (a random sequence is returned from the old python version) sequences from older Versions of Python), the algorithm for str and # generates a seed in a narrow range through the str and bytes algorithms. Bytes generates a narrower range of seeds. setstate (state) method of Random instance # method of Random instance Restore internal state from object returned by getstate (). # Save the object obtained and returned by getstate. Shuffle (x, random = None) method of Random instance # Shuffle list x in place, and return None. # disrupt list x and return None x as required parameter Optional argument random is a 0-argument function returning a # Optional parameter is a function that returns a floating point between 0 and 1 without any parameters in [0.0, 1.0); if it is the default None, the # if the optional parameter is the default None, it will use random. random Function Method standard random. random will be used. if you have a better random number generator or a random number generator suitable for your application, you can use it instead The default one. Triangular (low = 0.0, high = 1.0, mode = None) method of Random instance # Triangular distribution of the Random instance method. # triangular distribution, also known as Simpson distribution or triangle distribution Continuous distribution bounded by given lower and upper limits, # The triangle distribution is the continuous probability distribution of low limit, high limit, and average by default. And having a given mode value in-. http://en.wikipedia.org/wiki/Triangular_distribution uniform (a, B) method of Random instance # method of random instance Get a Random number in the range [a, B) or [a, B] depending on rounding. # If a random number is obtained between a and B, a and B can be obtained. If B can be obtained, we can see that B's rounding a and B must have two parameters. The integer type can be a floating point type, math is used for the method B I know. ceil this method returns a Random number vonmisesvariate (mu, kappa) method of Random instance # method of the Random instance Circular data distributio N. # cyclic data distribution or von meters' 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 has CES to a uniform random angle over the range 0 to 2 * pi. # mu is the measurement of position. Its gathering is between 0-2 * pi, and kappa is the measurement of concentration. It must be greater than or equal to 0. # If kappa is equal to 0, the distribution is even. In the case of a small K, the distribution is approximately even, and its location measurement is between 0 and 2 * pi. Weibo ullvariate (alpha, beta) method of Random instance Weibo distribution. # Boolean distribution alpha is the scale parameter and beta is the shape parameter. # λ> 0 is the scale parameter and k> 0 is the shape parameter. # Here, alpha is the scale parameter, beta is a shape parameter, also known as Weber distribution or Boolean distribution. It is the theoretical basis of Reliability Analysis and life-time test. Boolean distribution: It is widely used in reliability engineering and is especially suitable for the distribution of wear accumulative failures of electromechanical products. Because it can use probability values to easily deduce its distribution parameters, it is widely used in data processing of various life-cycle tests.

Summary

The above is all the details about the code of the Python3 Random module in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.