Python One of the built-in modules random
======================
random Kuo is Python Standard library for generating random numbers in , The list of included functions is as follows :
seed、random、getstate、setstate;randint、getrandbits、randrange、choice、shuffle、sample;uniform、triangular、betavariate、expovariate、gammavariate、gauss、lognormvariate、normalvariate、vonmisesvariate、paretovariate、weibullvariate.undefined Find words variate The frequency of occurrence is relatively high , But it's a variable .Basic random functions
seed Function initializes a random seed , The default is the current system time .
random function Generate a [0.0,1.0) Between random decimals .
The specific code is as follows :
import random random.seed(10) x = random.random() print(x)
What needs to be explained is random.seed function , adopt seed function You can generate the same random number each time , 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 , however x And y It's the same .
0.5714025946899135 0.5714025946899135
getstate Function to record the state of the random number generator ,setstate Function to restore the generator to the last recorded state .
# Record the state of the generator
state_tuple = random.getstate()
for i in range(4):
print(random.random())
print("*"*10)
# After passing in the parameter, the previous state is restored
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 functions
random There are several extended random functions :
randint`、`getrandbits`、`randrange`、`choice`、`shuffle`、`sample
randint Generate a [x,y] Integers within an interval .
randrange Generate a [m,n) In the interval with k Is a random integer of step size .
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 ,randint The function prototype is as follows :
random.randint(start,stop)
Parameters start Represents the minimum value , Parameters stop Represents the maximum value , It's closed at both ends , That is to say start and stop Can be obtained .
randrange The function prototype is as follows :
random.randrange(start,stop,step)
If the function is called with only one parameter , The default is 0 To the parameter value , The function and randint The difference lies in , The function is left closed and right open , The last parameter is the step size .
Look up 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 Generate a k Random integers that are longer than special , The actual output is k A decimal number converted from a binary number .
choice Randomly select an element from a sequence .
x = random.getrandbits(5) print(x) # The length of the generation is 00000-11111
getrandbits(k) The function can be simply described as follows : Output one $[0,2^k-1]$ A random integer in the range ,k It means 2 The number of decimal digits .
choice It's simpler , Returns a random element from the list .
import random my_list = ["a", "b", "c"] print(random.choice(my_list))
shuffle The function is used to sort elements in a sequence at random , And the original sequence was modified .
sample Functions are used to randomly select... From a sequence or set k A choice , The original sequence is the same .
my_list = [1,2,3,4,5,6,7,8,9] random.shuffle(my_list) print(my_list)
shuffle Functions can only be used for variable sequences , Immutable sequence ( As tuple ) There will be errors .
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 more , Focus on some important and common functions .
uniform Generate a [a,b] Between random decimals , Using equal probability distribution .
betavariate Generate a [0,1] Between random decimals , use beta Distribution .
triangular Generate a [low,high] Between random decimals , Using a triangular distribution .
In the use of uniform You need to pay attention to , If a<b, Then generate a b-a Decimal between .
for i in range(3): print(random.uniform(4, 1))
Here's how to generate random numbers , It's just that the underlying core algorithms are different .
、、、、、、、.
expovariate: Generate a (0,∞) Random integer between , An index distribution ;gammavariate: use gamma Distribution ;gauss: Using Gauss ( young cute boy ) Distribution ;lognormvariate: Lognormal distribution ;normalvariate: normal distribution ;vonmisesvariate: Von Mises distribution ;paretovariate: Pareto distribution ;weibullvariate: Weber distribution .The summary of this blog
This blog has learned Python Knowledge points related to random numbers in , I hope it helped you .