Python built-in function random

1. random. random()

1.1 Function

Randomly generate floating point numbers between [0, 1)

1.2 sample code
import random
print(random.random())
print(random.random())
print(random.random())
print(random.random())
print(random.random())
#结果
0.5766197578353556
0.57247327891827
0.4462615472400666
0.11451953086256073
0.36761261496444264

Two, random.randint(a,b)

2.1 Function

Randomly generate an integer in the range [a,b].

2.2 Sample code
import random
print(random.randint(0, 20))
print(random.randint(0, 20))
print(random.randint(0, 20))
print(random.randint(0, 20))
print(random.randint(0, 20))
#结果
18
11
20
16
20

三、random.randrange(a,b,step)

3.1 Function
  • Without specifying a step, an integer in the range [a, b) is randomly generated.
  • Specifying step, step as the step size will further limit the range of [a, b), for example, randrange(0,11,2) means to generate random even numbers in the range of [0,11).
  • If a is not specified, it starts from 0 by default.
3.2 Sample code
import random
print(random.randrange(0, 10,))
print(random.randrange(0, 10))
print(random.randrange(0, 10, 2))
print(random.randrange(0, 10, 2))
print(random.randrange(0, 10, 3))
print(random.randrange(0, 10, 3))
#结果
3
4
2
2
6
6

四、random.uniform(a,b)

4.1 Function

Generate a random floating point number in the range [a,b].

4.2 Sample code
import random
print(random.uniform(0, 5))
print(random.uniform(0, 5))
print(random.uniform(0, 5))
print(random.uniform(0, 5))
print(random.uniform(0, 5))
1.3417377618818764
0.11974945860308173
3.2462974509594766
2.3097156739199463
1.9782236542891152

5. random.choice(seq)

5.1 Function

Randomly select a piece of data from a non-empty sequence and bring it back. The sequence can be list, tuple, str, or set. If the sequence is empty, an IndexError will pop up.

5.2 Sample code
import random
print(random.choice('AutomaticDriving')) 
print(random.choice('AutomaticDriving')) 
print(random.choice('AutomaticDriving')) 
print(random.choice('AutomaticDriving')) 
#结果
D
o
u
c

六 、random.choices(population,weights=None,*,cum_weights=None,k=1)

6.1 Function

Randomly select k times of data from the cluster, return a list, and you can set the weight.

Note that each selection will not affect the original sequence, and each selection is based on the original sequence.

6.2 Parameter Explanation
  • population: cluster
  • weights: relative weight
  • cum_weights: accumulated weights
  • k: selection times
6.3 Sample code
import random
a = [1,2,3,4,5]
#1
print(random.choices(a,k=5))
#2
print(random.choices(a,weights=[0,0,1,0,0],k=5))
#3
print(random.choices(a,weights=[1,1,1,1,1],k=5))
#4
print(random.choices(a,cum_weights=[1,1,1,1,1],k=5))
#结果
[1, 2, 1, 4, 1]
[3, 3, 3, 3, 3]
[3, 5, 2, 3, 2]
[1, 1, 1, 1, 1]
6.4 Interpretation of sample code
  • #1: Repeat the output 10 times. The probability of each member in list a is basically the same.
  • #2: Repeat the output 10 times and get [3,3,3,3,3] results for each output.
  • #3: Repeat the output 10 times. The probability of each member in list a is basically the same.
  • #4: Repeat the output 10 times and get [1,1,1,1,1] results for each output.
  1. The parameter weights sets the relative weight, and its value is a list. After setting, the probability of each member being extracted is determined.
    For example, weights=[1,2,3,4,5], then the probability of the first member is P=1/(1+2+3+4+5)=1/15.
  2. cum_weights sets cumulative weights, and Python will automatically convert relative weights to cumulative weights, that is, if you directly give cumulative weights, then you don’t need to
    give relative weights, and Python omits one step of execution.
    For example, weights=[1,2,3,4], then cum_weights=[1,3,6,10],
    it is not difficult to understand why the output of cum_weights=[1,1,1,1,1] is all the first member 1 up.

Seven, random.sample(population,k)

7.1 Function

Select k elements from the cluster population and return a list, the cluster can be list, tuple, str, set.
The difference with random.choices(): one is to select k times, the other is to select k times, the choice of k times is equivalent to selection and then put back, and the selection of k is not put back after selection. Therefore, the k value of random.sample() cannot exceed the number of elements in the cluster.

7.2 Sample code
import random
a = ['SAIC', 'FAW', 'Geely', 'chengcheng', 'chengan']
print(random.sample(a, 3))
print(random.sample(a, 3))
print(random.sample(a, 3))
['Geely', 'chengan', 'chengcheng']
['chengan', 'FAW', 'chengcheng']
['FAW', 'chengcheng', 'SAIC']

Guess you like

Origin blog.csdn.net/LiuXF93/article/details/122523732