def makeRandyLine(char1='X', char2='Y', char3='Z'): ''' returns a line of characters passed in char1 is repeated from 3 to 7 times, chosen randomly char2 is repeated from 5 to 22 times, chosen randomly char3 is repeated from 2 to 14 times, chosen randomly ''' return char1*( random.randomInt( 3, 7 ) ) + \ char2*( random.randomInt( 5, 22 ) ) + \ char3*( random.randomInt( 2, 14 ) )
def birthday_problem(amount_of_students): classes = [ len(set([randomInt(1, 365) for _ in range(amount_of_students)])) for _ in range(100) ] total = sum(classes) return (amount_of_students * 100 - total) / (amount_of_students * 100)
def attack(self, enemy): if self.character_name == 'hero': randomInt = random.randint(1, 5) if randomInt == 1: self.power = self.power * 2 print(f"You do {self.power} damage to the {enemy.character_name}.") if enemy.character_name != 'zombie': enemy.health -= self.power if enemy.character_name == 'medic': randomInt = random.randint(1, 5) if randomInt == 1: enemy.health = enemy.health + 2 if enemy.character_name == 'shadow': randomInt = random.randomInt(1, 10) if randomInt != 1: print("The shadow dodged your attack!") enemy.health = 1 else: print("The shadow is vanquished!") elif (self.character_name == 'goblin' or self.character_name == 'zombie'): print( f"The {self.character_name} does {self.power} damage to you.")
def move(): data = bottle.request.json # TODO: Do things with data while(True): head = snakes.coords x = random.randomInt(1, 4) if x == 1 and head[0] != 0: return { 'move': 'north', 'taunt': 'battlesnake-python!' } elif x == 2 and head[0] != data.height: return { 'move': 'south', 'taunt': 'battlesnake-python!' } elif x == 3 and head[1] != data.width: return { 'move': 'east', 'taunt': 'battlesnake-python!' } elif x == 4 and head[1] != 0: return { 'move': 'west', 'taunt': 'battlesnake-python!' } else: continue
def random_army_selection(player_name = "Comp", player_army_Ids = [], player_money = unit.max_army_cost): if(player_money < 1): return #Here the max no of army units that he can get is as myuch money he has leftover. Since min amount is 1$ number_of_army_units = randomInt(1, player_money) while(player_money > 0 and number_of_army_units > 0): #There can be cases where number of units may not reach max number of army unit the player can have #Since the max cost per unit is greater than 1$ if(extended_features.extended_army_mode_enabled and player_money > 1): #because max cost of a unit in extended army is greater than 1 random_unit_ID = randomInt(1, unit.max_extended_unit_ID) else: random_unit_ID = randomInt(1,unit.max_basic_unit_ID) player_army_Ids.append(random_unit_ID) army_unit = unit.army_units[random_unit_ID] player_money -= army_unit[unit.unit_cost_pos] number_of_army_units -= 1 display_utilities.game_menu_title() display_utilities.display_player_details(player_name, player_army_Ids, player_money) return get_player_details(player_name,player_army_Ids,player_money)
def placeApple(self): import random apple_h = random.randomInt(0, 640) apple_w = random.randomInt(0, 480)
def start(self): food_not_present = True food = [0,0] score = -1 gameover = 0 #seeds for random function a0 = 12345 b0 = 34567 c0 = 12789 d0 = 23893 e0 = 56788 #turn all pixels on screen off self.bb.screen.clear(0) #write name of game on screen self.bb.screen.lcd.text('BOILER SNAKE', 18,26) #display screen self.bb.screen.lcd.show() time.sleep(1) #main loop that runs game while True: print("location: ", self.snake[0]) #if game over if gameover != 0: #turn all pixels on screen off self.bb.screen.clear(0) #write GAME OVER on screen self.bb.screen.lcd.text('GAME OVER', 27,26) #display screen self.bb.screen.lcd.show() time.sleep(1) self.snake = [[16,32],[12,32],[8,32],[4,32]] self.directions = [self.buttons.RIGHT, self.buttons.RIGHT, self.buttons.RIGHT, self.buttons.RIGHT] return #if winning score of 15 is met if score == 15: #turn all pixels on screen off self.bb.screen.clear(0) #write You win! on screen self.bb.screen.lcd.text('You win!', 27, 26) #display screen self.bb.screen.lcd.show() return #if snake eats food or food is not present if (self.snake[0] == food) or food_not_present: #generate random food location x_food, a0, b0, c0, d0, e0 = random.randomInt(X_MIN, X_MAX, a0, b0, c0, d0, e0) y_food, a0, b0, c0, d0, e0 = random.randomInt(Y_MIN, Y_MAX, a0, b0, c0, d0, e0) food = [int(x_food/4)*4, int(y_food/4)*4] score += 1 #adding unit to snake if not food_not_present: if self.directions[-1] == self.buttons.UP: self.snake.append([self.snake[-1][0], self.snake[-1][1]+4]) elif self.directions[-1] == self.buttons.DOWN: self.snake.append([self.snake[-1][0], self.snake[-1][1]-4]) elif self.directions[-1] == self.buttons.LEFT: self.snake.append([self.snake[-1][0]+4, self.snake[-1][1]]) elif self.directions[-1] == self.buttons.RIGHT: self.snake.append([self.snake[-1][0]-4, self.snake[-1][1]]) self.directions.append(self.directions[-1]) food_not_present = False #update score and food position self.bb.screen.clear(0) self.bb.screen.lcd.text('SCORE:', 37, 0) self.bb.screen.lcd.text(str(score), 84, 0) self.displaySolidUnit(food) self.checkButtonPress() #display snake for i in range(0, len(self.snake)): self.displayHollowUnit(self.snake[i]) self.bb.screen.lcd.show() #increase gameover gameover += self.updateSnake() #update direction of snake self.updateDirections() #determine speed of snake based on score if score <= 9: time.sleep((50-(score*5))/1000) else: time.sleep((50-(49))/1000) #garbage collection gc.collect()
def getPiece(): pieceCount = len(piecesArray) pieceNum = random.randomInt(0, pieceCount - 1) return piecesArray[pieceNum]
def refresh(self): if self.partitionUpdateFlag: # every time refresh, we only refresh current partition # first update partition if necessary if len(self.episodeLengths) == self.capacity: actualPartitionNum = self.partitionNum else: actualPartitionNum = int( math.floor(self.totalLength / self.partitionLength)) accumLength = np.cumsum(self.episodeLengths) selectPartition = random.randomInt(0, actualPartitionNum) episodeStart = selectPartition * self.partitionLength episodeEnd = (selectPartition + 1) * self.paritionLength self.partitionRange = (accumLength[episodeStart] - self.episodeLength[episodeStart], accumLength[episodeEnd - 1]) else: episodeStart = 0 episodeEnd = len(self.memory) self.partitionRange = (0, self.totalLength) transitions = AugumentedTransition(*zip( *self.memory[self.partitionRange[0]:self.partitionRange[1]])) reward = torch.tensor(transitions.reward, device=self.device, dtype=torch.float32).unsqueeze( -1) # shape(batch, 1) batchSize = reward.shape[0] # get select partition of memory if self.stateProcessor is not None: state, _ = self.stateProcessor(transitions.state, self.device) nonFinalNextState, nonFinalMask = self.stateProcessor( transitions.next_state, self.device) else: # state = torch.tensor(transitions.state, device=self.device, dtype=torch.float32) nonFinalMask = torch.tensor(tuple( map(lambda s: s is not None, transitions.next_state)), device=self.device, dtype=torch.uint8) nonFinalNextState = torch.tensor( [s for s in transitions.next_state if s is not None], device=self.device, dtype=torch.float32) QNext = torch.zeros(batchSize, device=self.device, dtype=torch.float32) QNext[nonFinalMask] = self.net(nonFinalNextState).max(1)[0].detach() offSet = accumLength[episodeStart] - self.episodeLengths[episodeStart] for episode in reversed(range(episodeStart, episodeEnd)): startIdx = accumLength[episode] - self.episodeLengths[episode] endIdx = accumLength[episode] # for each episode, we update the lambda return if self.memory[endIdx - 1].next_state is None: self.memory[idx].process_reward = self.memory[idx].reward else: self.memory[idx].process_reward = self.memory[ idx].reward + self.gamma * QNext[endIdx - 1 - offSet] for idx in reversed(range(startIdx, endIdx - 1)): self.memory[idx].process_reward = self.memory[idx].reward +self.gamma * ( self.lamb * self.memory[idx + 1].process_return + (1 - self.lamb) * QNext[idx - offSet])
def get_random_number(self): value = random.randomInt(len(self.items)) value1 = random.choice(self.items) self.items.remove(value[0]) return value[0]
def predict(self, X_test): outcome = [random.randomInt(0, 1) for i in range(len(X_test))] return pd.DataFrame(outcome)