Browse Source

Added _rand_subset method to MiniGridEnv

Maxime Chevalier-Boisvert 7 years ago
parent
commit
0ad70c61dd
1 changed files with 17 additions and 0 deletions
  1. 17 0
      gym_minigrid/minigrid.py

+ 17 - 0
gym_minigrid/minigrid.py

@@ -808,6 +808,23 @@ class MiniGridEnv(gym.Env):
         idx = self._rand_int(0, len(lst))
         return lst[idx]
 
+    def _rand_subset(self, iterable, num_elems):
+        """
+        Sample a random subset of distinct elements of a list
+        """
+
+        lst = list(iterable)
+        assert num_elems <= len(lst)
+
+        out = []
+
+        while len(out) < num_elems:
+            elem = self._rand_elem(lst)
+            lst.remove(elem)
+            out.append(elem)
+
+        return out
+
     def _rand_color(self):
         """
         Generate a random color name (string)