Random number generator with exclusions

I have 9x9 numpy array filled with numbers [1;9]. I need to pick random position and assign this cell as 0 until I get certain difficulty value. 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. Is there any functions I can use or methods I may implement? Thank you in advance.

Upvotes: 0

Views: 162

Answers (1)

Marat
Marat

Reputation: 15738

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

Upvotes: 1

Related Questions