In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "what are the high-frequency use of the python random module", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "what is the high-frequency use of the python random module?"
In order to facilitate everyone's understanding and memory, the methods are classified:
Integer function
Random.randrange (stop) randomly returns an integer between [0, stop-1]
Random.randrange (start, stop, step) randomly returns integers between [start, stop-1]. Step is an incremental count. List a formula and you will understand: (randomly generated value-start)% step=0
Random.randint (start, stop) randomly returns integers between [start, stop]
Random.getrandbits (bits) is used for integers that randomly correspond to bits bits to randomly return integers between [0,2 bits bits-1].
Finally, it is important to note that the above methods can also randomly return negative numbers, as long as the parameters inherit negative numbers. Here's an example:
Integers between # 0-2 print (random.randrange (3)) # 1-299 The growth base is a 5 print (random.randrange (1,300,5)) # 1-20 integer print (random.randint (1,20)) #-3 to 0 negative print (random.randint (- 3,0)) #-3 to-1 negative print (random.randrange (- 3) ) # returns the number print (random.randrange (1)) between 1x 2-1 # returns the number print (random.getrandbits (32)) # # returns the value 0 117 0-1 3150567570
Floating point function
Random.random () randomly returns a floating-point number between [0Phon1).
Random.uniform (aformab) randomly returns floating-point numbers between [amaine b)
In fact, the above two methods are also in line with the mathematical random distribution pattern, in order to facilitate your understanding, I will list them separately here (different from the following mathematical distribution pattern), so that you can be less hesitant when randomly generating floating-point numbers.
# 1. Generate a random floating point number print (random.random ()) # 2 between 0 and 1. Generate a random floating point number print (random.uniform (10,500)) print (random.uniform (40,10)) # return a value of 0.7833926665736892 150.2805875943869 39.88236257691508
Sequence function
As the name implies, the random object of the sequence function is the list.
Random.choice (list) randomly returns an element from list.
Random.choices (list, weights,k) is the equivalent of an upgraded version of random.choice (). Weights is the weight, and the weight is set for each element of the list to change the random probability. K returns several elements. Note that this method returns a list.
Random.sample (population, k) 3.6A new feature for returning random, unduplicated samples from the population list. As mentioned earlier, if you want to make a lottery system, you can use this method to transform it.
Random.shuffle (list) randomly scrambles the elements in the list list.
The corresponding examples are as follows:
# randomly select the upgraded version of an element print (random.choice ([1, 2, 3, 4, 5]) # random.choice from a given sequence, and you can also specify the weight print (random.choices ([1, 2, 3, 4, 5], [10, 15, 45, 50, 60])) # to disrupt a sequence Sample from python3.6 's new method list_test = [2, 3, 4, 5, 6] random.shuffle (list_test) print (list_test) # and randomly select several elements print (random.sample ([1, 2, 3, 4, 5, 6, 7], KF3)) # return values 3 [5] [6, 3, 4, 2, 5] [6, 1, 7]
Control the behavior of random
Random.seed (a=None, version=2) sets the seed value of random. How do you understand the seed value? Just think of one sentence: "reap what you sow, reap what you sow", so if you have the same seed value, the result will be the same. If you randomly set random.seed to the same value twice now before calling random to randomly generate data, the randomly generated value will be the same.
Random.getstate () is simple enough to return the current state of the random generator.
Random.setstate (state), which is used to set the state of random setstate in conjunction with the getstate method, can be used to restore the state of random.
The corresponding examples are as follows:
# verify seed random.seed (1) print (random.randrange (1000)) random.seed (1) print (random.randrange (1000)) # verify the status of random state = random.getstate () print (random.randrange (1000)) print (random.randrange (1000)) random.setstate (state) print (random.randrange (1000)) # return result 137 582 867 582
Mathematical distribution model
The following methods are all related to the concept of mathematical distribution. To be honest, all the relevant things learned in university have been returned to the teacher. I checked the data to understand the knowledge of different mathematical distribution, but it is a bit difficult to understand. But in fact, for the people we use, we just need to understand that it can randomly return floating-point numbers according to certain rules.
Random.triangular (low, high, mode) returns random numbers in the probability distribution of triangular distribution.
Random.betavariate (alpha, beta) returns a random number between 0 and 1 with the probability distribution of beta distribution.
Random.expovariate () returns random numbers with exponential distribution.
Random.gammavariate (alpha, beta) returns random numbers with the probability distribution of gamma distribution.
Random.gauss (mu, sigma) returns random numbers in the probability distribution of Gaussian distribution.
Random.lognormvariate (mu, sigma) returns random numbers in the probability distribution of lognormal distribution.
Random.normalvariate (mu, sigma) is similar to Gaussian distribution.
Random.vonmisesvariate (mu, kappa) returns random numbers with the probability distribution of von Mises distribution.
Random.paretovariate (alpha) returns random numbers as the probability distribution of Pareto (Pareto) distribution.
Random.weibullvariate (alpha, beta) returns random numbers with the probability distribution of Weibull (Wilbur) distribution.
In fact, we do not need to fully remember the above methods, encounter specific use scenarios, know that there is a corresponding method, can check the document to find it.
Thank you for your reading, the above is the content of "what is the high-frequency use of the python random module?" after the study of this article, I believe you have a deeper understanding of the high-frequency use of the python random module, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.