python-random模块
import random
#1.random()
a = random.random()
print(a) # Random float: 0.0 <= x < 1.0
#2.uniform()
print(random.uniform(2.5,10.0)) # Random float: 2.5 <= x < 10.0
#3.expovariate()
print(random.expovariate(1 / 5)) # Interval between arrivals averaging 5 seconds
#4.randrange()
print(random.randrange(10)) # Integer from 0 to 9 inclusive
print(random.randrange(0,101,2)) # Even integer from 0 to 100 inclusive
#5.choice()
print(random.choice(['win','lose','draw'])) # Single random element from a sequence
#6.choices()
#random.choices(population, weights=None, *, cum_weights=None, k=1)
a = [5,9,20,10,2,8]
print(random.choices(a,weights=[1,1,1,3,1,1],k=3))
#7.randint()
print(random.randint(12,20)) # Returns a random integer N such that a <= N <= b.
#8.sample()
print(random.sample([10, 20, 30, 40, 50, 50], k=4)) # For random sampling without repetition
#9.shuffle() # Randomly rearrange the list
a = [5,9,20,10,2,8]
random.shuffle(a) # Return None
print(a)
#10.seed()
random.seed(1)
print(random.random())
print(random.getrandbits(4))
保持好奇心!

浙公网安备 33010602011771号