python学习-random()函数、getstate()和setstate()伪随机数攻击
1、random.randint(x,y)--随机生成x-y范围内的任一整数
2、使用getstate()和setstate()可以复现random生成的随机数
3、使用random改善昨天的小游戏
def Game():
import random
count = int(input("please insert the value:"))
answer = random.randint(1,10)
while count > 0:
guess = int(input("please insert your value:"))
if guess == answer:
print("right")
print("over")
break
elif guess > answer:
print("too big")
else:
print("too small")
count -= 1
if count == 0:
print("no chance,over")
if __name__ == "__main__":
Game()
4、使用getstate()和setstate()复现随机数
import random
x = random.getstate()
print(random.randint(1,10))
print(random.randint(1,10))
print(random.randint(1,10))
random.setstate(x)
print(random.randint(1,10))
print(random.randint(1,10))
print(random.randint(1,10))