def execute(self, solution_list: List[S]) -> S:
if solution_list is None:
raise Exception("The solution list is null")
elif len(solution_list) == 0:
raise Exception("The solution is empty")
elif not self.comparator_list:
raise Exception("The list of comparators is empty")
winner = None
if len(solution_list) == 1:
winner = solution_list[0]
else:
for comparator in self.comparator_list:
winner = self.__winner(solution_list, comparator)
if winner is not None:
break
if not winner:
i = random.randrange(0, len(solution_list))
winner = solution_list[i]
return winner
python类randrange()的实例源码
def generate_name():
alphabet = 'abcdefghijklmnopqrstuvwxyz'
wordlist = []
with open ("words", 'r') as fo:
for word in fo:
if len(word) < 5:
continue
else:
wordlist.append(word.strip())
namelength = random.randrange(4)
pref = random.choice(wordlist)[0:3]
pref = pref[0].upper() + pref[1:3]
if random.random() > 0.5:
suff = random.choice(wordlist)[-3:]
else:
suff = random.choice(wordlist)[:3]
for i in range(namelength):
pref = pref + random.choice(alphabet)
name = pref + suff
return name
def main():
xp = []
yp = []
for i in range(3):
xp.append(random.randrange(15))
yp.append(random.randrange(15))
points = []
for i in range(3):
points.append(Point(xp.pop(),yp.pop()))
print([i.get_cords() for i in points])
tri = Triangle(points.pop(),points.pop(),points.pop())
mid = tri.centre()
for y in range(15):
for x in range(15):
if (tri.a.x == x and tri.a.y == y):
print('A',end="")
elif (tri.b.x == x and tri.b.y == y):
print('B',end="")
elif (tri.c.x == x and tri.c.y == y):
print('C',end="")
elif (mid.x == x and mid.y == y):
print(" .",end="")
else:
print(" ",end="")
print("")
def __init__(self, location, name="default"):
self.name = name
self.location = location
self.owner = random.randrange(10)
def __init__(self,seed):
random.seed(seed)
self.ISLAND_FACTOR = 1.13 # 1.0 means no small islands; 2.0 leads to a lot
self.bumps = random.randrange(1,7)
self.startAngle = random.uniform(0,2*math.pi)
self.dipAngle = random.uniform(0,2*math.pi)
self.dipWidth = random.uniform(0.2,0.7)
def test():
for i in range(8):
a.next, b.next, sel.next = randrange(8), randrange(8), randrange(2)
yield delay(2)
def __init__(self, data_, n_clusters):
self.position = []
if not self.position:
for i in range(len(data_)):
self.position.append(random.randrange(n_clusters))
print(self.position)
self.velocity = [0 for clusterPoint in self.position]
self.S = [0 for clusterPoint in self.position]
self.best = deepcopy(self.position)
self.bestfit = 0
def __init__(self, data_, n_clusters, genes):
self.genes = genes
if not self.genes:
for i in range(len(data_)):
self.genes.append(random.randrange(n_clusters))
def mutation(self, pmut, n_clusters):
for g, gene in enumerate(self.genes):
if uniform(0, 1) <= pmut:
oldgene = self.genes[g]
# garante mutation diferente
while self.genes[g] == oldgene:
self.genes[g] = random.randrange(n_clusters)
def spawn(self):
new_element = 4 if randrange(100) > 89 else 2
(i,j) = choice([(i,j) for i in range(self.width) for j in range(self.height) if self.field[i][j] == 0])
self.field[i][j] = new_element
def move(xe, ye, button):
global mouseSpeed
if button != (1 or 2):
return
ms = mouseSpeed
randSpeed = (random.randrange(mouseSpeed) / 2.0 + mouseSpeed) / 10.0
# get Cur mouse position
xs,ys = pyautogui.position()
humanWindMouse(xs, ys, xe, ye, 1, 5, 10.0/randSpeed, 15.0/randSpeed, 10.0*randSpeed)
mouseSpeed = ms
# click here. Add Code
def set_random_action(self):
self.random_action = random.randrange(0,8+1)
def dnd(self):
self._dnd = True
if not self.id:
self.id = 'grid-%d' % random.randrange(0, 9999)
return self
def add_tab(self, link, panel, id=''):
if not id:
id = 'tab-%s' % random.randrange(0, 9999)
else:
id = 'tab-%s' % id
link.klass.append('node')
link.attr['href'] = '#%s-%s' % (id, random.randrange(0, 9999))
tab = '<li id="tab-%s">%s</li>' % (id, link.html())
self.kss._append_script("%s.find('.tabs').append(%s)" % (self.kss._selector, self.kss._escape_value(tab)), False, False)
self.kss.append('<div id="%s" class="tabBody hidden">%s</div>' % (link.attr['href'][1:], panel.html()))
def randombytes(n):
"""Return n random bytes."""
# Use /dev/urandom if it is available. Fall back to random module
# if not. It might be worthwhile to extend this function to use
# other platform-specific mechanisms for getting random bytes.
if os.path.exists("/dev/urandom"):
f = open("/dev/urandom")
s = f.read(n)
f.close()
return s
else:
L = [chr(random.randrange(0, 256)) for i in range(n)]
return "".join(L)
def _random_getnode():
"""Get a random node ID, with eighth bit set as suggested by RFC 4122."""
import random
return random.randrange(0, 1<<48L) | 0x010000000000L
def uuid1(node=None, clock_seq=None):
"""Generate a UUID from a host ID, sequence number, and the current time.
If 'node' is not given, getnode() is used to obtain the hardware
address. If 'clock_seq' is given, it is used as the sequence number;
otherwise a random 14-bit sequence number is chosen."""
# When the system provides a version-1 UUID generator, use it (but don't
# use UuidCreate here because its UUIDs don't conform to RFC 4122).
if _uuid_generate_time and node is clock_seq is None:
_buffer = ctypes.create_string_buffer(16)
_uuid_generate_time(_buffer)
return UUID(bytes=_buffer.raw)
global _last_timestamp
import time
nanoseconds = int(time.time() * 1e9)
# 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
timestamp = int(nanoseconds//100) + 0x01b21dd213814000L
if _last_timestamp is not None and timestamp <= _last_timestamp:
timestamp = _last_timestamp + 1
_last_timestamp = timestamp
if clock_seq is None:
import random
clock_seq = random.randrange(1<<14L) # instead of stable storage
time_low = timestamp & 0xffffffffL
time_mid = (timestamp >> 32L) & 0xffffL
time_hi_version = (timestamp >> 48L) & 0x0fffL
clock_seq_low = clock_seq & 0xffL
clock_seq_hi_variant = (clock_seq >> 8L) & 0x3fL
if node is None:
node = getnode()
return UUID(fields=(time_low, time_mid, time_hi_version,
clock_seq_hi_variant, clock_seq_low, node), version=1)