百人百钱问题

问题:在一间房间里有一百个人,每个人身上有一百块钱,每过一分钟就随机让一个人让他将自己的一块钱给随机的另一个人,问在经过很长时间后,所以人的钱分布情况如何?

我们大多是人的直接感觉应该是所有人的钱应该都在一百元左右,出现偏差也应该是个别现象。但是事实是怎样的呢?作为一个程序员,我觉到这应该要show一下计算机模拟的结果了

用python写出模拟算法

from random import randint
import pygame


def swap(arr):
    one = randint(0, 99)
    two = randint(0, 98)
    if one <= two:
        two += 1
    arr[one] -= 1
    arr[two] += 1


pygame.init()
width = 805
height = 1000
width_center = width // 2
height_center = height // 2
cell_width = 8
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
arr = [100] * 100
running = True
count = 0
while running:

    # clock.tick(60)
    swap(arr)
    if count % 10000 == 0:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                running = False
                break
        screen.fill((255, 255, 255))
        pygame.draw.line(screen, (0, 255, 255), (0, height_center + 20), (width, height_center + 20))
        pygame.draw.line(screen, (0, 255, 255), (0, height_center), (width, height_center))
        pygame.draw.line(screen, (0, 255, 255), (0, height_center - 20), (width, height_center - 20))

        index = 0
        for i in arr:
            pygame.draw.line(screen, (255, 0, 0) if i > 0 else (0, 0, 0), (index * cell_width + 3, height_center),
                             (index * cell_width + 3, height_center - i // 10),
                             cell_width - 1)
            index += 1

        mi = min(arr)
        ma = max(arr)
        font = pygame.font.SysFont("arial", 50, True)
        text = font.render("Max:{},Min:{}".format(str(ma), str(mi)), True, (0, 0, 0))
        screen.blit(text, (10, height - 100))
        pygame.display.update()
    count += 1
结果1
结果2

结果还是令人费解的,最终会出现很大的“贫富差距”,也许这可能是电脑产生的随机数是伪随机数的原因吧。

你可能感兴趣的:(百人百钱问题)