Random function instructions

8 commonly used random functions
Basic random number functions seed() random()
Extended random number functionsrandint() getrandbits() uniform() randrange() choice() shuffle()

a =random.seed(10)
Set the seed to 10, and assign it to the variable a for the purpose of testing.
Generate the sequence corresponding to seed 10. Initialize the given random number seed. The default is the current system time seed (a=None) to
random.random()
generate a random decimal between [0.0-1.0]
If the seed used is 10, then the first random decimal generated must be a fixed value of 0.57
# 如果种子是相同的,那么产生的随机数也是相同的
# 所以我们可以复现/再现程序运行的过程
# 不使用固定种子,那么则是使用系统时间,系统时间精确到微秒,比较难再现,所以认定为是随机
randint(10,100)
randint(a,b) generates an integer between [a,b]
randrange(10,100,10)
randrange([a,b,c]) Generate random integers between [a,b] with c as the step size
getrandbits(16)#
getrandbits(a) Generate a random integer with a bit length
uniform(10,100)
uniform(a,b) Generate random decimals between [a,b] with 16 digits precision The decimal
choice([3,6,9])
choice(seq) randomly selects an element from the sequence seq
shuffle([3,6,9])
shuffle(seq) randomly arranges the elements in the sequence seq, and returns the shuffled sequence

#伪随机数导入
import random
#基本随机数函数seed() random()
#————————————如果你想去掉固定种子请注释下面这行
a =random.seed(10) 
b =random.random() 
#————————————如果你想去掉固定种子请注释下面这行
print("a的值为空",a,"a的类型为没有类型",type(a))
print("产生的b值为",b,"类型为浮点类型",type(b))


# 扩展随机数函数 randint() getrandbits() uniform() randrange() choice() shuffle()
c =random.randint(10,100) #生成[a,b]之间的整数
d =random.randrange(10,100,10) #[a,b,c]生成[a,b]之间以c为步长的随机整数
e= random.getrandbits(16)# getrandbits(a)生成一个a比特长的随机整数
f=random.uniform(10,100) #uniform(a,b) 生成[a,b]之间的随机小数 精度为16位的小数
g=random.choice([3,6,9])#choice(seq)从序列seq中随机选择一个元素

print(c,"c:种子决定了随机值")
print(d,"d:种子决定了随机值")
print(e,"e:种子决定了随机值")
print(f,"f:种子决定了随机值")
print(g,"g:种子决定了随机值")
#random.shuffle([3,6,9])#shuffle(seq)将序列seq中元素随机排列,返回打乱后的序列
#合并三行  除非你很有必要,才这样写
s=[3,6,9];random.shuffle(s);print(s,"s:种子决定了随机值")
# 常规写法
x=[3,6,9]
random.shuffle(x)
print(x,"x:种子决定了随机值")

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/114794101