def action(self): dX = 0 dY = 0 if self._world.isHexWorld: pass #TODO else: dX = random.randrange(-1, 2, 1) if (self._posX + dX < 0) or (self._posX + dX >= self._world.worldSizeX): dX = 0 dY = random.randrange(-1, 2, 1) if (self._posY + dY < 0) or (self._posY + dY >= self._world.worldSizeY): dY = 0 field = None if dX != 0 or dY != 0: field = self._world.isEmpty(self._posX + dX, self._posY + dY) self._posX += dX self._posY += dY if field is not None and field.alive: self.collision(field, dX, dY)
def action(self): maxX = self._world.worldSizeX maxY = self._world.worldSizeY i = random.randrange(-1, 2, 1) for a in range(0, 3): j = random.randrange(-1, 2, 1) for b in range(0, 3): if self._posY + i < 0 or self._posY + i >= maxY or (i == 0 and j == 0): if ++j == 2: j = -1 break if 0 <= self._posX + j < maxX: organism = self._world.isEmpty(self._posX + j, self._posY + i) if organism is None: self._posX += j self._posY += i return elif organism.strength <= self._strength or type( organism) == type(self): self._posX += j self._posY += i super().collision(organism, j, i) return
def escape(self, organism, dX, dY): import WorldSim.Organisms.Plants.Plant if isinstance(organism, WorldSim.Organisms.Plants.Plant.Plant): return False if random.randrange(0, 100, 1) <= Antelope.__escapeChance: return super().escape(organism, dX, dY) return False
def changecolor(): "Changement aléatoire de la couleur du tracé" global coul, coul2 #pal=['purple','cyan','maroon','green','red','blue','orange','yellow'] # => génère un nombre aléatoire de 0 à 7 pal = [ 'purple', 'cyan', 'maroon', 'green', 'red', 'blue', 'orange', 'yellow' ] c = randrange(1, 4) # => couleurs cyan, marron et green coul = pal[c] coul2 = pal[4] #couleur rouge pour la croix
def action(self): if random.randrange(0, 100, 1) < 20: maxX = self._world.worldSizeX maxY = self._world.worldSizeY for i in range(-1, 2, 1): for j in range(-1, 2, 1): if self._posY + i < 0 or self._posY + i >= maxY: break if (self._posX + j >= 0) and ( self._posX + j < maxX) and self._world.isEmpty( self._posX + j, self._posY + i) is None: self._world.addOrganism( self.clone(self._posX + j, self._posY + i)) self._world.setMessage(self.getName() + " has grown") return
# 1 Question import Lib.random as random random.random() # Random float x, 0.0 <= x < 1.0 # 0.37444887175646646 random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0 # 1.1800146073117523 random.randint(1, 10) # Integer from 1 to 10, endpoints included # 7 random.randrange(0, 101, 2) # Even integer from 0 to 100 # 26 random.choice('abcdefghij') # Choose a random element # 'c' items = [1, 2, 3, 4, 5, 6, 7] random.shuffle(items) items [7, 3, 2, 5, 6, 4, 1] random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements [4, 1, 5] # 2 Question def func(x, ans): if (x == 0): return 0 else: return func(x - 1, x + ans)
def ajouter_pion(): "dessiner un pion au hasard sur le damier" # tirer au hasard les coordonnées du pion : x = c/2 + randrange(10) * c y = c/2 + randrange(10) * c cercle(x, y, c/3, 'red')
def action(self): if random.randrange(0, 100, 1) <= Tortoise.__moveChance: super().action()
def generator(): lltype = randrange(1, 3, 1) linkedlist = LinkedList() first = Node(randrange(1, 101, 1)) linkedlist.head = first tail = first if lltype == 1: more = randrange(1, 101, 1) while more != 100: nnode = Node(randrange(1, 101, 1)) tail.next = nnode tail = nnode more = randrange(1, 101, 1) return linkedlist else: print('snail') loop = randrange(1, 1001, 1) while loop < 985: nnode = Node(randrange(1, 101, 1)) tail.next = nnode tail = nnode loop = randrange(1, 1001, 1) loop_to = tail more = randrange(1, 101, 1) while more != 100 and more != 99: nnode = Node(randrange(1, 101, 1)) tail.next = nnode tail = nnode more = randrange(1, 101, 1) tail.next = loop_to return linkedlist