为了账号安全,请及时绑定邮箱和手机立即绑定

为什么蟒蛇随机生成相同的结果?

为什么蟒蛇随机生成相同的结果?

Smart猫小萌 2022-09-27 09:51:22
我用Python写了一个相当简单的程序。代码如下:import pygameimport timefrom math import *from random import *from pygame.locals import *def mutateBrain(brain):    a = 0    for element in brain:        brain[a][0] = element[0] + (1 * (0.5 - random()))        brain[a][1] = element[1] + (1 * (0.5 - random()))        a = a + 1    return brain;def generateFirstBrain():    genbrain = []    h = 0;    while randint(0,5) != 0:        asd = [2 * random(), 2 * random()]        genbrain.insert(h, asd)        h = h + 1    return genbrainpygame.init()width, height = 640, 480screen=pygame.display.set_mode((width, height))screen.fill(pygame.Color(255,255,255))pygame.draw.rect(screen,(255,0,0),(310,0,30,30))movesa = generateFirstBrain()movesb = generateFirstBrain()movesc = generateFirstBrain()cola = (255,255,0)colb = (255,0,255)colc = (0,255,255)while 1:    movesa = mutateBrain(movesa)    movesb = mutateBrain(movesb)    movesc = mutateBrain(movesc)    step = 0    acurrentx = 320    acurrenty = 240    bcurrentx = 320    bcurrenty = 240    ccurrentx = 320    ccurrenty = 240    totalsn = 0    if (len(movesa) >= len(movesb)) and (len(movesa) >= len(movesc)):        totalsn = len(movesa)    elif (len(movesb) >= len(movesa)) and (len(movesb) >= len(movesc)):        totalsn = len(movesb)    else:        totalsn = len(movesc)    for g in range(totalsn):        screen.fill(pygame.Color(255,255,255))        pygame.draw.rect(screen,(255,0,0),(305,0,30,30))        try:            acurrentx = acurrentx + 1 - movesa[step][0]            acurrenty = acurrenty + 1 - movesa[step][1]        except IndexError:            acurrentx = acurrentx在这里,在第一个 while 循环结束时,无论我运行突变大脑函数多少次,movel 总是得到相同的值。但它会发生变化,因此随机应该存在问题。有人可以帮我这个吗?谢谢。
查看完整描述

1 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

使用这个工作:

突变大脑 = λ x: [[y + 0.5 - 随机 () 对于 y 在 a 中] 对于 x 中的 a]


查看完整回答
反对 回复 2022-09-27
  • 1 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号