I completely changed my previous answer. Here is a class that wraps a random number generator (with extra seed) with a list. This is a minor improvement over FJ as it provides deterministic behavior for testing. The choice()
call in the first list should not affect the second list and vice versa:
class rlist (): def __init__(self, lst, rg=None, rseed=None): self.lst = lst if rg is not None: self.rg = rg else: self.rg = random.Random() if rseed is not None: self.rg.seed(rseed) def choice(self): return self.rg.choice(self.lst) if __name__ == '__main__': rl1 = rlist([1,2,3,4,5], rseed=1234) rl2 = rlist(['a','b','c','d','e'], rseed=1234) print 'First call:' print rl1.choice(),rl2.choice() print 'Second call:' print rl1.choice(),rl2.choice()
source share