Eraser, a new series of fun Internet advanced webworms, let’s Be More Pythonic together.

Random for Python built-in modules

The random library is the standard Python library for generating random numbers. It contains the following list of functions:

  • Basic random function:seed,random,getstate,setstate;
  • Extended random function:randint,getrandbits,randrange,choice,shuffle,sample;
  • Distributed random function:uniform,triangular,betavariate,expovariate,gammavariate,gauss,lognormvariate,normalvariate,vonmisesvariate,paretovariate,weibullvariate. Find the wordsvariateThe frequency is relatively high, but is a variable meaning.

12.1 Basic random functions

12.1.1 Seed and Random functions

The seed function initializes a random seed, which defaults to the current system time. The random function generates a random decimal between [0.0,1.0].

The specific code is as follows:

import random

random.seed(10)

x = random.random()
print(x)
Copy the code

The seed function generates the same random number each time, as shown in the following code:

import random

random.seed(10)
x = random.random()
print(x)

random.seed(10)
y = random.random()
print(y)
Copy the code

The values are different from code to code, but x and y are the same.

0.5714025946899135
0.5714025946899135
Copy the code

12.1.2 getstate () and a setstate (state)

The getState function is used to record the state of the random number generator, and the setstate function is used to restore the generator to the state of the last record.

Record the state of the generator
state_tuple = random.getstate()
for i in range(4) :print(random.random())
print("*"*10)
Restore the previous state after passing the parameter
random.setstate(state_tuple)
for j in range(4) :print(random.random())
Copy the code

The random number output is the same twice.

0.10043296140791758
0.6183668665504062
0.6964328590693109
0.6702494141830372
**********
0.10043296140791758
0.6183668665504062
0.6964328590693109
0.6702494141830372
Copy the code

12.2 Extending random Functions

Random extended random functions are as follows:

'randint', 'getrandbits',' randrange ', 'choice', 'shuffle', 'sample'Copy the code

12.2.1 randint and randrange

Randint generates an integer in the range [x,y]. Randrange generates a random integer with k steps in the range [m,n].

The test code is as follows:

x = random.randint(1.10)
print(x)

y = random.randrange(1.10.2)
print(y)
Copy the code

These two functions are relatively simple. The randint function prototype is as follows:

random.randint(start,stop)
Copy the code

The start parameter indicates the minimum value and the stop parameter indicates the maximum value. Both are closed intervals, that is, both start and stop can be obtained.

The prototype of randrange function is as follows:

random.randrange(start,stop,step)
Copy the code

If the function is called with only one argument, the default is 0 to the value of that argument. This function differs from Randint in that the function is left closed and right open, and the last argument is the step size.

To see the effect, you can copy the following code to run:

for i in range(3) :print("*"*20)
    print(random.randrange(10))
    print(random.randrange(5.10))
    print(random.randrange(5.100.5))
Copy the code

12.2.2 getrandbits (k) and choice (seq)

Getrandbits generates a random integer k bits long, which actually outputs a decimal number converted from a k bit binary number. Choice Selects a random element from the sequence.

x =  random.getrandbits(5)
print(x)
The generated length is 00000-11111
Copy the code

The getrandbits(k) function can be simply described as follows: outputs a random integer in the range [0,2k−1][0,2^k-1][0,2k−1], where k represents the number of digits in base 2.

Choice is simpler, returning a random element from the list.

import random

my_list = ["a"."b"."c"]

print(random.choice(my_list))
Copy the code

12.2.3 shuffle (seq) and sample (pop, k)

The shuffle function is used to randomly sort the elements in the sequence, and the original sequence is modified. The sample function is used to randomly select K selections from a sequence or set, with the original sequence unchanged.

my_list = [1.2.3.4.5.6.7.8.9]
random.shuffle(my_list)

print(my_list)
Copy the code

The shuffle function can only be used for mutable sequences. Immutable sequences (such as tuples) have errors.

my_list = ["Dream".eraser.1.2[3.4]]
print(my_list)
ls = random.sample(my_list, 4)
print(ls)
Copy the code

12.3 Distributed random function

This section covers a bit more, highlighting some important and common functions.

12.3.1 Uniform (A, B), betavariate and correlation

Uniform generates a random decimal between [A, B], using an equiprobability distribution. Betavariate generates a random fraction between [0,1] and uses a beta distribution. Improve the trigonometry by generating a random decimal number between low and high.

When using UNIFORM, note that if a< B, then a decimal between B and A is generated.

for i in range(3) :print(random.uniform(4.1))
Copy the code

12.3.2 Other distributed random functions

The following are all methods to generate random numbers, but the underlying core algorithm is different. 、、、、、、、.

  1. expovariate: Generate a(0, up)Between random integers, exponential distribution;
  2. gammavariate: Gamma distribution is adopted;
  3. gauss: Gaussian distribution is used;
  4. lognormvariate: log-positive distribution;
  5. normalvariate: normal distribution;
  6. vonmisesvariate: Von Mises distribution;
  7. paretovariate: Pareto distribution;
  8. weibullvariate: Weber distribution.

12.4 Summary of this blog post

This blog has taken a look at random numbers in Python.

Blogger ID: Dream eraser, hope you like, comment, favorites.