random is a module for Python to generate pseudo-random numbers. The random seed defaults to the system clock. The methods in the module are analyzed below:
This is a function that generates integer random numbers. The parameter start represents the minimum value, and the parameter stop represents the maximum value. , the values at both ends can be obtained;
The time complexity of the function algorithm is: O(1)
Core source code:
return self.randrange(a,b+1) #调用randrange函数来处理
Example:
import random for i in range(20): print(random.randint(0,10),end=' ')
Result:
1 1 7 5 10 1 4 1 0 8 7 7 2 10 6 8 6 0 3 1
is also a random integer function with optional parameters
Only When there is one parameter, the default random range is from 0 to this parameter, closed first and open later;
When there are two parameters, it represents the minimum and maximum values, closed first and open later
When there are three parameters, it represents the minimum value, maximum value and step size, closed first and open later
Time complexity of function algorithm: O(1)
Core source code:
return istart+istep*self._randbelow(n) #调用randbelow函数处理
Instance:
import random for i in range(10): print(random.randrange(10),end=' ') #产生0到10(不包括10)的随机数 print("") for i in range(10): print(random.randrange(5,10),end=' ') #产生5到10(不包括10)的随机数 print("") for i in range(10): print(random.randrange(5,100,5),end=' ') #产生5到100(不包括100)范围内的5倍整数的随机数
Result:
1 1 2 4 4 3 4 6 1 4 6 6 5 7 8 9 6 6 6 5 30 50 20 40 75 85 25 65 80 95
A random Selection function, seq is a non-empty set, randomly selects an element in the set for output, and the type of element is not limited.
Core source code:
i=self._randbelow(len(seq)) #由randbelow函数得到随机地下标 return seq[i]
Function algorithm time responsibility: O(1)
Example:
import random list3=["mark","帅",18,[183,138]] for j in range(10): print(random.choice(list3),end=' ')
Code:
mark 帅 [183, 138] 18 mark 18 mark 帅 帅 [183, 138]
This function forms any floating point number from 0.0 to 1.0, closed on the left and open on the right, with no parameters.
Example:
import random for j in range(5): print(random.random(),end=' ')
Run result:
0.357486615834809 0.5928029747238529 0.37053940107869987 0.3802224543848519 0.9741990956161711
One can initialize the random number generator function, n represents a random seed; when n=None, the random seed is the system time. When n is other data, such as int, str, etc., the provided data is used as the random seed. The random number sequence generated at this time is fixed. .
Example:
import random random.seed("mark") for j in range(20):#无论启动多少次程序,输出的序列不变 print(random.randint(0,10),end=' ')
Result:
4 1 10 5 6 2 8 5 5 10 7 2 9 6 2 6 0 5 10 10
getstate() function is used To record the state of the random number generator, the setstate(state) function is used to restore the generator to the last recorded state.
Example:
import random tuple1=random.getstate()#记录生成器的状态 for i in range(20): print(random.randint(0,10),end=' ') print() random.setstate(tuple1)#传入参数回复之间的状态 for i in range(20): print(random.randint(0,10),end=' ')#两次输出的结果一致
Result:
5 7 9 9 10 10 2 3 7 1 1 6 1 7 1 1 7 4 2 2 5 7 9 9 10 10 2 3 7 1 1 6 1 7 1 1 7 4 2 2
Shuffle the incoming collection sequence operation. It can only be used for mutable sequences, such as strings and lists. An error will be reported for immutable sequences such as tuples. random is used to select the out-of-order operation method, such as random=random.
Core source code:
for i in reversed(range(1,len(x))): j=randbelow(i+1) x[i],x[j]=x[k],x[i]
Time complexity of function algorithm: O(n)
Example:
import random lists=['mark','帅哥',18,[183,138]] print(lists) random.shuffle(lists,random=None) print(lists)
Result:
['mark', '帅哥', 18, [183, 138]] ['帅哥', 18, 'mark', [183, 138]]
The population parameter is a sequence, such as a list, tuple, set, string, etc.; k elements are randomly selected from the set to form a new sequence, The original sequence will not be changed.
Worst time complexity: O(n*n)
Example:
import random lists=['mark','帅哥',18,[183,138]] lists2=random.sample(lists,3) print(lists) print(lists2)
Result:
['mark', '帅哥', 18, [183, 138]] ['mark', [183, 138], '帅哥']
A function that generates a floating-point number between parameters a and b. If a>b, it generates a floating-point number between b and a.
Core source code:
return a+(b-a)*self.random()
Time complexity: 0(1)
Example:
import random for i in range(5): print(random.uniform(10,1))
Result:
2.8826090956524606 1.5211191352548408 3.2397454278562794 4.147879756524251 6.532545391009419
The above is the detailed content of Analysis of random module in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!