random of Python built-in module
Random library is a standard library for generating random numbers in Python. The list of functions included is as follows:
- Basic random functions: seed, random, getstate, setstate;
- Extended random functions: random, getrandbits, random, choice, shuffle, sample;
- Distributed random functions: uniform, triangular, betavariate, expand, gammavariate, gauss, lognormalvary, normalvary, von misesvariate, paretovary, Weibull vary.
It is found that the word variable appears more frequently, but it means variable.
Basic random function
seed and random functions
The seed function initializes a random seed. The default is the current system time.
The random function generates a random decimal between [0.0,1.0].
The specific codes are as follows:
import random random.seed(10) x = random.random() print(x)
Among them, random The seed function can generate the same random number every time through the seed function, for example, the following code:
import random random.seed(10) x = random.random() print(x) random.seed(10) y = random.random() print(y)
The values obtained on different codes are different, but x and y are the same.
0.5714025946899135 0.5714025946899135
getstate() and 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 last recorded state.
# Status of the record generator state_tuple = random.getstate() for i in range(4): print(random.random()) print("*"*10) # Restore the previous state after passing in parameters random.setstate(state_tuple) for j in range(4): print(random.random())
The output random number is consistent twice.
0.10043296140791758 0.6183668665504062 0.6964328590693109 0.6702494141830372 ********** 0.10043296140791758 0.6183668665504062 0.6964328590693109 0.6702494141830372
Extended random function
Random extended random functions are as follows:
randint`,`getrandbits`,`randrange`,`choice`,`shuffle`,`sample
randint and randange
randint generates an integer within the [x,y] interval.
randrange generates a random integer with k as the step size in the [m,n) interval.
The test code is as follows:
x = random.randint(1,10) print(x) y = random.randrange(1,10,2) print(y)
These two functions are relatively simple. The randint function prototype is as follows:
random.randint(start,stop)
The parameter start represents the minimum value and the parameter stop represents the maximum value. Both ends are closed intervals, that is, start and stop can be obtained.
The prototype of the randrange function is as follows:
random.randrange(start,stop,step)
If there is only one parameter when calling a function, the default is from 0 to the parameter value. The difference between this function and randint is that the function is closed on the left and open on the right, and the last parameter is step size.
To view 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))
getrandbits(k) and choice(seq)
getrandbits generates a k-bit long random integer, and the actual output is the decimal number converted from a k-bit binary number.
choice randomly selects an element from the sequence.
x = random.getrandbits(5) print(x) # The generated length is 00000-11111
The getrandbits(k) function can be simply described as follows: output a random integer in the range of $\ [0,2^k-1 \] $, and K represents the number of binary digits.
choice is simpler, returning a random element from the list.
import random my_list = ["a", "b", "c"] print(random.choice(my_list))
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 choices from the sequence or set, and the original sequence remains unchanged.
my_list = [1,2,3,4,5,6,7,8,9] random.shuffle(my_list) print(my_list)
The shuffle function can only be used for variable sequences, and errors will occur for immutable sequences (such as tuples).
my_list = ["Dream", "eraser", 1, 2, [3, 4]] print(my_list) ls = random.sample(my_list, 4) print(ls)
Distributed random function
This part involves a lot, focusing on some important and common functions.
uniform(a,b), betavariate, and triangular functions
uniform generates a random decimal between [a,b], using equal probability distribution.
betavariate generates a random decimal between [0,1], using beta distribution.
Triangular generates a random decimal between [low,high], which adopts triangular distribution.
When using uniform, it should be noted that if a < B, a decimal between B and a will be generated.
for i in range(3): print(random.uniform(4, 1))
Other distributed random functions
The following are the methods of generating random numbers, but the underlying core algorithms are different.
,,,,,,,.
- Expand: generate a random integer between (0, ∞) and exponential distribution;
- Gamma variable: adopt gamma distribution;
- gauss: Gaussian (Zhengtai) distribution;
- Lognormal vector: lognormal vector distribution;
- Normal variable: Zhengtai distribution;
- Von misesvariate: Von Mises distribution;
- paretovariate: Pareto distribution;
- Weibull variable: Weber distribution.
Summary of this blog
This blog has learned some knowledge about random numbers in Python. I hope it will be helpful to you.