我一直在尝试这个代码来获得一个随机种子,但总是失败。在
import string
import random
import time
import sys
from random import seed
possibleCharacters = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' .,!?;:'
target = input("Enter text: ")
attemptThis = ''.join(random.choice(possibleCharacters) for i in range(len(target)))
attemptNext = ''
completed = False
generation = 0
while completed == False:
print(attemptThis)
attemptNext = ''
completed = True
for i in range(len(target)):
if attemptThis[i] != target[i]:
completed = False
attemptNext += random.choice(possibleCharacters)
else:
attemptNext += target[i]
generation += 1
attemptThis = attemptNext
time.sleep(0)
time.sleep(seed(1))
print("Operation completed in " + str(generation) + " different generations!")
错误总是这样:
^{pr2}$我试过其他函数——randint,pi,生成一个随机数,然后用一个随机数来决定。在
我是必须对这个数字进行硬编码,还是有办法让它产生随机延迟?在
还可以使用
random.randint(a, b)
函数-生成随机整数。seed
函数只是初始化随机数生成器。在种子的类型是“无”和时间。睡觉()需要整数。 要发送一个随机整数作为睡眠参数,您可以尝试:
你需要为此导入randim libraby。在
random.seed()
函数总是返回None
,但是time.sleep()
需要一个数字(睡眠的秒数)。在尝试
random.randint()
生成一个随机整数:相关问题 更多 >
编程相关推荐