The random module of python3 is used to generate random numbers, strings, etc.
1.random.random()
generates a random decimal in the range [0,1)
import random
t=random.random()
print(t)
# 0.2635740247512115
2.random.int(min,max)
generates an integer random number of [min,max]
import random
t=random.randint(1,10)
print(t)
#10
3.random.uniform(min,max)
generates a min~max random floating point number, the interval may not be an integer
import random
t=random.uniform(1.4,10)
print(t)
#7.4683172499214585
4.random.randrange(min,max,k)
generates a random integer with an interval of k from min to max
import random
t=random.randrange(1,10,3)
print(t)
#7
5.random.shuffle(L) shuffles
the order of sequence L and rearranges it
import random
ll=[1,2,3,4,5]
random.shuffle(ll)
print(ll)
# [3, 2, 4, 1, 5]
6.random.choice(L)
Obtain a random element from the sequence L
import random
ll=[1,2,3,4,5]
t=random.choice(ll)
print(t)
#3
- random.choices(L,k)
get k random elements from the sequence L
import random
ll=[1,2,3,4,5]
t=random.choices(ll,k=3)
print(t)
#[2, 2, 2]
8.random.sample(L,k)
Obtain fragments of the specified length from the sequence L and sort them randomly
import random
ll=[1,2,3,4,5]
t=random.sample(ll,k=3)
print(t)
#[4, 2, 3]