python - 使用累积概率绘制时遇到死循环

标签 python linux python-3.x

问题描述如下:根据元素"p"的不同概率从5×9二维矩阵中随机采样,共N = 15个不重复的2D会选择索引值,概率“p”的二维数组已经硬编码,二维“p”数组的和为1,但很多情况下我的程序会落入死循环,在elif语句中,反复检查问题,一开始以为是随机数种子的问题。然后,每次创建新的随机数时,使用time.time()重置随机种子,但仍然存在问题, 所以我来问问题出在哪里?为什么会这样?这是什么原因?

import random
import time


def pick_pisotions(how_many):
    result_positions = []
    p = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0.09200879765395895, 0.10227272727272728, 0.0988514173998045, 0.010019550342130987, 0, 0, 0],
         [0, 0, 0.0024437927663734115, 0.17424242424242423, 0.44477028347996095, 0.07539100684261975, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 0, 0, 0, 0, 0]]
    temp_position_list = []  # 2D positions, each item just like this format :(row, col)
    temp_p_list = []  # probability of be chosen of 2D array above
    for row in range(5):
        for col in range(9):
            temp_position_list.append((row, col))
            temp_p_list.append(p[row][col])
    print("sum =", sum(temp_p_list))

    seed_index = 0
    while True:  # choose N no-repeating positions, ()
        seed_index += 1
        # random.seed(time.time())
        random.seed(seed_index)
        length_of_result = len(result_positions)
        if length_of_result == how_many:  # enough
            return result_positions
        elif 0 <= length_of_result < how_many:  # not enough
            random_number = random.uniform(0, 1)
            cumulative_probability = 0.0
            for item_position, item_probability in zip(temp_position_list, temp_p_list):
                cumulative_probability += item_probability  #
                print(" test random_number = ", random_number)
                print(" test cumulative_probability = ", cumulative_probability)

                if random_number <= cumulative_probability:
                    result_positions.append(item_position)
                    result_positions = list(set(result_positions))  #
                    break
                else:
                    print("test random_number > cumulative_probability")
                    # pass
        else:
            print("error :pick_tiffs()")


pick_pisotions(15)

死循环总是在这里发生:

if random_number <= cumulative_probability:
    result_positions.append(item_position)
    result_positions = list(set(result_positions))  #
    break

我的母语不是英语,所以我的一些句子可能听起来很奇怪 :) 并等待您的帮助~

最佳答案

抱歉,我只是找到问题所在:

result_positions = list(set(result_positions))  #

此行删除重复项,我所有的二维“p”数组只有 8 个非零项,因此“length_of_result”永远不会匹配超过 8 个的长度(例如 15) 我以前没有注意到这个问题。

为了获得高质量的随机数,我发现我可以使用 secrets.SystemRandom(3.6 版中的新功能。一个用于生成随机数的类,使用操作系统提供的最高质量资源。)

from random import SystemRandom
from random import Random

system_random = SystemRandom(Random())
random_number = system_random.random()

关于python - 使用累积概率绘制时遇到死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45919412/

相关文章:

python-3.x - ZeroMQ 卡在 python 多处理类/对象解决方案中

python - 在此程序中使用 try 和 except 在此 python 中

python 2.7.5 请求和证书验证失败

python - 几乎相同的 Tesseract 图像解析不同

python - 检测邻近词

linux - 如何在 PulseAudio 中获取播放进程列表

c - 树莓派 2 上的 valgrind 被 libtasn1 读取无效

python - 如何离线设置 ChatGPT 进行开发

python - 具有中央中继服务器的多线程套接字

linux - 如何在 linux c 上添加 dir.h 库