内置函数
import random
# 随机小数[0, 1)
print(random.random()) # 0.8121215001773937
# 随机小数[a, b),指定区间
print(random.uniform(1,5)) # 3.2253060854754354
# 数据整数[a, b]
print(random.randint(1,5)) # 3
# 随机范围,取偶数
print(random.randrange(0,50,2)) # 14
# 随机取值string
print(random.choice("python")) # n
# 随机取值list
lst = ["one", "two", "three"]
print(random.choice(lst)) # one
# 随机取值tuple
tp = ("one", "two", "three")
print(random.choice(tp)) # two
# 取样
print(random.sample("abcdefg", 3))
# ['e', 'g', 'c']
# 洗牌
lst = [1, 2, 3, 4, 5,6]
random.shuffle(lst)
print(lst) # [3, 2, 1, 6, 5, 4]
生成四位随机验证码
# 方案一:
checkcode = ""
for i in range(4):
current = random.randrange(0,4)
# 生成字母
if i == current:
tmp = chr(random.randint(65, 90)) # A - Z
# 生成数字
else:
tmp = str(random.randint(0,9))
checkcode += tmp
print(checkcode) # 715C
# 方案二:
import string
codes = string.ascii_letters + string.digits
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
sample = random.sample(codes, 4) # ['c', 'l', 'p', '5']
check_code = "".join(sample)
print(check_code) # clp5
help(random)
FUNCTIONS
choice(seq) method of Random instance
Choose a random element from a non-empty sequence.
randint(a, b) method of Random instance
Return random integer in range [a, b], including both end points.
random(...) method of Random instance
random() -> x in the interval [0, 1).
randrange(start, stop=None, step=1, _int=) method of Random instance
Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
sample(population, k) method of Random instance
Chooses k unique random elements from a population sequence or set.
Returns a new list containing elements from the population while
leaving the original population unchanged. The resulting list is
in selection order so that all sub-slices will also be valid random
samples. This allows raffle winners (the sample) to be partitioned
into grand prize and second place winners (the subslices).
Members of the population need not be hashable or unique. If the
population contains repeats, then each occurrence is a possible
selection in the sample.
To choose a sample in a range of integers, use range as an argument.
This is especially fast and space efficient for sampling from a
large population: sample(range(10000000), 60)
seed(a=None, version=2) method of Random instance
Initialize internal state from hashable object.
None or no argument seeds from current time or from an operating
system specific randomness source if available.
For version 2 (the default), all of the bits are used if *a* is a str,
bytes, or bytearray. For version 1, the hash() of *a* is used instead.
If *a* is an int, all bits are used.
shuffle(x, random=None) method of Random instance
Shuffle list x in place, and return None.
Optional argument random is a 0-argument function returning a
random float in [0.0, 1.0); if it is the default None, the
standard random.random will be used.
uniform(a, b) method of Random instance
Get a random number in the range [a, b) or [a, b] depending on rounding.