简体   繁体   English

具有排除项的随机数生成器

[英]Random number generator with exclusions

I have 9x9 numpy array filled with numbers [1;9].我有 9x9 numpy 数组,其中填充了数字 [1;9]。 I need to pick random position and assign this cell as 0 until I get certain difficulty value.我需要随机选择 position 并将此单元格分配为 0,直到获得一定的难度值。 By the way, my table should satisfy certain criterias and functions were checked on multiple tests.顺便说一句,我的表应该满足某些标准,并且在多次测试中检查了功能。

while current_difficulty > self.difficulty:
        i = np.random.randint(0, self.board_size, 1)
        j = np.random.randint(0, self.board_size, 1)

        if self.unsolved[i, j] != 0 and self.__is_solvable():
            self.unsolved[i, j] = 0
            current_difficulty -= 1

What I need is get random pairs of values (i and j) that won't repeat over time.我需要的是获得不会随时间重复的随机值对(i 和 j)。 Is there any functions I can use or methods I may implement?有什么我可以使用的功能或我可以实现的方法吗? Thank you in advance.先感谢您。

Basic idea: generate all unique pairs (hopefully the board is not too big), then shuffle:基本思路:生成所有唯一对(希望棋盘不要太大),然后洗牌:

from itertools import product
# ...
indexes = list(product(np.arange(self.board_size), np.arange(self.board_size)))
np.random.shuffle(indexes)  # unique indexes in random order
for i, j in indexes:
     if some_check(i, j):
         break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM