def test_choice_distribution(self): a = ('a', 'b', 'c', 'd') p = (0.5, 0.2, 0.2, 0.1) sample = choices_distribution(a, p)[0] assert sample in a random_state = json.loads((TEST_DIR / 'random_state.json').read_text()) random_state[1] = tuple(random_state[1]) random.setstate(random_state) samples = choices_distribution(a, p, length=100) a_pop = len([i for i in samples if i == 'a']) b_pop = len([i for i in samples if i == 'b']) c_pop = len([i for i in samples if i == 'c']) d_pop = len([i for i in samples if i == 'd']) boundaries = [] tolerance = 5 for probability in p: boundaries.append( [100 * probability + tolerance, 100 * probability - tolerance]) assert boundaries[0][0] > a_pop > boundaries[0][1] assert boundaries[1][0] > b_pop > boundaries[1][1] assert boundaries[2][0] > c_pop > boundaries[2][1] assert boundaries[3][0] > d_pop > boundaries[3][1]
def test_choice_distribution(self): a = ('a', 'b', 'c', 'd') p = (0.5, 0.2, 0.2, 0.1) sample = choices_distribution(a, p)[0] self.assertTrue(sample in a) with open(os.path.join(TEST_DIR, 'random_state.json'), 'r') as fh: random_state = json.load(fh) random_state[1] = tuple(random_state[1]) random.setstate(random_state) samples = choices_distribution(a, p, length=100) a_pop = len([i for i in samples if i == 'a']) b_pop = len([i for i in samples if i == 'b']) c_pop = len([i for i in samples if i == 'c']) d_pop = len([i for i in samples if i == 'd']) boundaries = [] tolerance = 5 for probability in p: boundaries.append( [100 * probability + tolerance, 100 * probability - tolerance]) self.assertTrue(boundaries[0][0] > a_pop > boundaries[0][1]) self.assertTrue(boundaries[1][0] > b_pop > boundaries[1][1]) self.assertTrue(boundaries[2][0] > c_pop > boundaries[2][1]) self.assertTrue(boundaries[3][0] > d_pop > boundaries[3][1])
def _random_ipv4_address_from_subnets(self, subnets, weights=None, network=False): """ Produces a random IPv4 address or network with a valid CIDR from within the given subnets using a distribution described by weights. :param subnets: List of IPv4Networks to choose from within :param weights: List of weights corresponding to the individual IPv4Networks :param network: Return a network address, and not an IP address :return: """ # If the weights argument has an invalid value, default to equal distribution try: subnet = choices_distribution(subnets, weights, random=self.generator.random, length=1)[0] except (AssertionError, TypeError): subnet = self.generator.random.choice(subnets) address = str( subnet[self.generator.random.randint( 0, subnet.num_addresses - 1, )], ) if network: address += '/' + str(self.generator.random.randint( subnet.prefixlen, subnet.max_prefixlen, )) address = str(ip_network(address, strict=False)) return address
def _select_factory(self, method_name): """ Returns a random factory that supports the provider method :param method_name: Name of provider method :return: A factory that supports the provider method """ factories, weights = self._map_provider_method(method_name) if len(factories) == 0: msg = "No generator object has attribute '{}'".format(method_name) raise AttributeError(msg) elif len(factories) == 1: return factories[0] if weights: factory = choices_distribution(factories, weights, length=1)[0] else: factory = random.choice(factories) return factory
def generate_waveform(cls) -> list[float]: return choices_distribution(cls._float_space, p=None, length=1000)