Python rand.randint一遍遍地返回相同的数字?

Python rand.randint一遍遍地返回相同的数字?,python,python-3.x,raspberry-pi,Python,Python 3.x,Raspberry Pi,我不太精通Python,所以可能我遗漏了一些明显的东西,但我不明白为什么我用来生成随机数的代码似乎每个周期都使用相同的数字。我将在下面介绍我的python代码,首先介绍一些基本的导入和类设置内容: import random class Static: # Settings (some shoudl be, you know, settings settings) PIXEL_COUNT = 144 current_pixels = [[0,0,0]]*144 target_pixels =

我不太精通Python,所以可能我遗漏了一些明显的东西,但我不明白为什么我用来生成随机数的代码似乎每个周期都使用相同的数字。我将在下面介绍我的python代码,首先介绍一些基本的导入和类设置内容:

import random
class Static:
# Settings (some shoudl be, you know, settings settings)
PIXEL_COUNT = 144

current_pixels = [[0,0,0]]*144
target_pixels =  [[255,255,255]]*144

# Output bar
def bar(self,settings):

    # this is where the final output pixels will go
    pixels = [(0,0,0)]*self.PIXEL_COUNT

    for pixel_index in range(self.PIXEL_COUNT):
        self.update_pixel(pixel_index)

    # Debug
    print('Update current_pixels',self.current_pixels);

    for pixel_index in range(self.PIXEL_COUNT):
        pixels[pixel_index] = (self.current_pixels[pixel_index][0],self.current_pixels[pixel_index][1],self.current_pixels[pixel_index][2])

    return pixels

# Will process the pixel and the specified index
def update_pixel(self,index):
    rand = random.randint(0,255)
    for color in range(3):
        if self.current_pixels[index][color] > self.target_pixels[index][color]:
            self.current_pixels[index][color] -= 1
        elif self.current_pixels[index][color] < self.target_pixels[index][color]:
            self.current_pixels[index][color] += 1
        else:
            self.current_pixels[index][color] = rand

_inst = Static()
bar = _inst.bar
bar({})
随机导入
类别静态:
#设置(有些应该是设置)
像素计数=144
当前_像素=[[0,0,0]]*144
目标像素=[[255255255]]*144
#输出条
def栏(自身、设置):
#这就是最终输出像素的位置
像素=[(0,0,0)]*self.PIXEL\u计数
对于范围内的像素索引(自像素计数):
自我更新像素(像素索引)
#调试
打印(“更新当前像素”,自当前像素);
对于范围内的像素索引(自像素计数):
像素[像素索引]=(自当前像素[像素索引][0]、自当前像素[像素索引][1]、自当前像素[像素索引][2])
返回像素
#将处理像素和指定的索引
def更新_像素(自身,索引):
rand=random.randint(0255)
对于范围(3)内的颜色:
如果self.current_pixels[index][color]>self.target_pixels[index][color]:
自身当前像素[索引][颜色]-=1
elif self.current_pixels[index][color]

我很乐意把代码放在一把小提琴里,如果有人有一个不会让我注册使用它的。当我运行该代码时,我得到一个列表的输出,该列表包含144个包含相同编号的列表(所有列表中的所有编号都相同)。正如我所理解的代码,它应该有一系列不同的值(只有列表像素列表中的值应该匹配-白色静态的东西)。就像我说的,我对python非常陌生,所以它可能是一些基本的东西,但我不知道它可能是什么。有什么帮助吗?

您的问题是您的
if-else

if self.current_pixels[index][color] > self.target_pixels[index][color]:
        self.current_pixels[index][color] -= 1
elif self.current_pixels[index][color] < self.target_pixels[index][color]:
    self.current_pixels[index][color] += 1
else:
    self.current_pixels[index][color] = rand


您的问题是您的
if-else

if self.current_pixels[index][color] > self.target_pixels[index][color]:
        self.current_pixels[index][color] -= 1
elif self.current_pixels[index][color] < self.target_pixels[index][color]:
    self.current_pixels[index][color] += 1
else:
    self.current_pixels[index][color] = rand


请把你的代码写成一段代码,它们之间的语句作为内联注释,现在我无法理解这种格式的代码缩进@PhillipGoochPlease把你的代码写在一块中,它们之间的语句作为内联注释,现在我无法理解这种格式的代码缩进@PhillipGoochapter在将当前_像素提升到目标_像素后,将第一次循环,然后点击else并将其重置为新的随机值。在第一次循环后,每个像素仅迭代一次。如果您希望外部循环保证所有像素都达到else,那么范围内像素索引(self.pixel\u COUNT)的
将需要包装在范围内像素索引(255)的
中:
对不起,我不明白这将实现什么,为什么我要循环255?我只是希望它们在第一次同步时都会上升,然后给每个值一个0-255之间的随机值,在这一点之后,它们都会彼此独立地重置(因为它们会在不同的时间达到随机值)。在将当前_像素提升为目标_像素后,应点击else并将其重置为新的随机值。在第一次循环后,每个像素仅迭代一次。如果您希望外部循环保证所有像素都达到else,那么范围内像素索引(self.pixel\u COUNT)的
将需要包装在范围内像素索引(255)的
中:
对不起,我不明白这将实现什么,为什么我要循环255?我只是想让它们在第一次同步时都上升,然后给它们一个0-255之间的随机值,在这一点之后它们都会彼此独立地重置(因为它们会在不同的时间达到随机值)。
current_pixels = [[random.randint(0,255) for _ in range(3)] for _ in range(144)]