python - Ensuring minimum distance between choices -
#!/usr/bin/env python # -*- coding: utf-8 -*- import random, collections, time, os # pseudo-randomly select element table # ensuring minimum distance before same # element selected again def choice_gen(choices, min_dist): last_choices = collections.deque() while 1: choices = set(choices) c = random.choice(list(choices - set(last_choices))) new_min = int(len(choices)*6/10) if min_dist == new_min: last_choices.pop() min_dist=new_min last_choices.append(c) choices = yield c
i've kind of edited generator, minimum distance follows proportion:
len(choices):min_dist = 10:6
when value given result of proportion new_min int(len(choices)*6/10)
changes, deque goes increase in size of 1 element not popping. then, until new_min changes again, keeps popping, ensuring constant size of deque. @ least, that's should do. implementation right?
#!/usr/bin/env python # -*- coding: utf-8 -*- import random, collections # pseudo-randomly select element table # ensuring minimum distance before same # element selected again def choice_gen(choices, min_dist): last_choices = collections.deque() choices = set(choices) while 1: c = random.choice(list(choices)) choices.remove(c) last_choices.append(c) if len(last_choices) == min_dist: choices.add(last_choices.popleft()) yield c
Comments
Post a Comment