def randomSnack(rows, items): positions = item.body while True: x = random.randomrange(rows) y = random.randomrange(rows) if len(list(filter(lambda z: z.pos == (x, y), positions))) > 0: continue else: break return (x, y)
def update_waiting_time(self, state): rand = random.randomrange(0, 100) print(rand) if state == LKW_state_enum.LOADING: if rand < 10: return 16.0 elif rand < 30: return 10.0 elif rand > 30: return 12.0 elif state == LKW_state_enum.WEIGHTING: if rand < 20: return 24.0 elif rand < 35: return 18.0 elif rand > 35: return 16.0 elif state == LKW_state_enum.EMPTYING: if rand < 10: return 120 elif rand < 15: return 80 elif rand > 15: return 60 else: return 0
def random_item(n): a = len(n) for i in n: i = random.randomrange(n[0], n[a - 1]) b.append(i) return b
def find_sriov_nic(self, select_policy='first'): """ Find pci NICs which support SR-IOV and return a NIC name according to select policy ('auto', 'first') :param select_policy: the policy to select, 'auto', 'first' """ sr_iov_devs = [] pci_id = None pci_devs = self.find_pci_devices('Ethernet Controller') for dev in pci_devs: if self.is_pci_support_sriov(dev): sr_iov_devs.append(dev) if select_policy == 'first': pci_id = sr_iov_devs[0] elif select_policy == 'auto': pci_id = sr_iov_devs[random.randomrange(len(sr_iov_devs))] else: LOG.error("Invalid select policy") if pci_id is not None: all_nics = self.get_all_physical_nics() for nic in all_nics: output = self.run_command('ethtool -i %s' % nic).stdout if pci_id in output: LOG.info("Found device: %s => %s" % (pci_id, nic)) return (pci_id, nic) LOG.error("Failed to find SR-IOV nic")
def play_with_model(model): scores = [] choices = [] print("Playing wtih Trained Model.....") for each_game in range(10): score = 0 game_memory = [] prev_obs = [] env.reset() for _ in range(goal_steps): env.render() if len(prev_obs) == 0: action = random.randomrange(0, 2) else: #this clever call finds the index of the max argument #since predict will return something like [][0.23.., 0.76..]] #it will return 0 if the first is bigger, 1 if the second # which is the same as 'left' or 'right' in the action space action = np.argmax(model.predict([prev_obs])[0]) choices.append(action) new_observation, reward, done, info = env.step(action) prev_obs = new_observation game_memory.append([new_observation, action]) score += reward if done: break scores.append(score) print('Average Score', sum(scores) / len(scores)) print('Choice 1: {}, Choice 0: {}'.format( choices.count(1) / len(choices), choices.count(0) / len(choices)))
def process(oid, tag, value, **context): if not context['nextFlag'] and not context['exactMatch']: return context['origOid'], context['errorStatus'] # serve exact OIDs if context['setFlag']: return context['origOid'], context['errorStatus'] # read-only mode if oid not in settingsCache: settingsCache[oid] = dict([ x.split('=') for x in value.split(',') ]) if 'function' in settingsCache[oid]: f = getattr(math, settingsCache[oid]['function']) else: f = lambda x: x v = f((time.time() - booted) * float(settingsCache[oid].get('rate', 1))) * float(settingsCache[oid].get('scale', 1)) + float(settingsCache[oid].get('offset', 0)) d = int(settingsCache[oid].get('deviation', 0)) if d: v += random.randomrange(-d, d) if v < int(settingsCache[oid].get('min', 0)): v = int(settingsCache[oid].get('min', 0)) elif v > int(settingsCache[oid].get('max', 0xffffffff)): v = int(settingsCache[oid].get('max', 0xffffffff)) return oid, v
def DieOutcomes(): global playagain rolls = random.randrange( 0, 6) #this generates a random number between 1 and 6 if rolls == 1: print('Oh no, you rolled a 1, that isnt good.') print('') print('You wake up once again in a horrid and dark place.') print( 'You spend your few remaining days clawing around in the dark, eventually starving to death.' ) playAgain() elif rolls == 2: print('Ah yes, you rolled a 2.') print( 'I grant you the sword, Excalibur, to make you undefeateable in combat.' ) print('') win2() elif rolls == 3: print( 'A 3, allow me to turn back the passage of time. Of course, you won\'t be allowed to remember this. Bye!' ) print('') playagain = 'y' start() elif rolls == 4: print('A 4, now, let me explain.') print('') four() elif rolls == 5: print('') print( 'A 5, you have won the lottery. As promised, I grant you infinite wealth.' ) win3() elif rolls == 6: print('') print( 'No, no no, that is not supposed to happen. What makes you think you can roll a 6?' ) print('You know what that means, do you not?') print( 'But I have decided I can make an exception, so roll again, quickly, before he notices' ) print('') rolls == random.randomrange(0, 6)
def sendState(num=1): # Default number of copies to send is 1 for i in range(num): # If RAND_MOD == 1, always send if RAND_MOD == 1: sendUDP(str(STATE)) elif RAND_TYPE == True: # Drop 1 of n packets if random.randrange(RAND_MOD) != 0: sendUDP(str(STATE)) else: # Send 1 of n packets if random.randomrange(RAND_MOD) == 0: sendUDP(str(STATE)) return
import random as m from m import randomrange print(m.randomrange(1,10))
def img_from_link(url): name = random.randomrange(1, 1001) full_name = "img/" + str(name) + ".jpg" urllib.request.urlretrieve(url, full_name)
def randomize(l): for i in range(len(l)//2): j = random.randomrange(0, len(l), 1) k = random.randomrange(0, len(l), 1) (l[j], l[k]) = (l[k], l[j])
def most_common(L): # get an iterable of (item, iterable) pairs SL = sorted((x, i) for i, x in enumerate(L)) # print 'SL:', SL groups = itertools.groupby(SL, key=operator.itemgetter(0)) # auxiliary function to get "quality" for an item def _auxfun(g): item, iterable = g count = 0 min_index = len(L) for _, where in iterable: count += 1 min_index = min(min_index, where) # print 'item %r, count %r, minind %r' % (item, count, min_index) return count, -min_index # pick the highest-count/earliest item return max(groups, key=_auxfun)[0] def getRandom(): Randoms = list() Randoms.append(random.randrange(2, 15)) return Randoms RandomList = list() for i in range(8): RandomList.append(getRandom()) indexnum = random.randomrange(0,8) print RandomList[indexnum]
import random random.randomrange(1, 10)
def loadCsv(filename): lines=csv.reader(open(filename,"r")) dataset=list(lines) for i in range(len(dataset)): dataset[i]=[float(x) for x in dataset[i]] return dataset def splitDataset(dataset,splitRatio): trainSize=int(len(dataset)*splitRatio) trainSet=[] copy=list(dataset) while len(trainSet)<trainSize: index=random.randomrange(len(copy)) trainSet.append(copy.pop(index)) return[trainSet,copy] def separateByClass(dataset): separated={} for i in range(len(dataset)): vector=dataset[i] if(vector[-1]not in separated): separated[vector[-1]]=[] separated[vector[-1]].append(vector) return separated
for x in [2,4,6,8,10]: print(x) num_list = [[1,2,3],[13,45,63,54], [500,700,500,498]] for x in range(0,3): for y in range(0,3): print(num_list[x][y]) # A bit about while loop # They are going to be use when you don't have any idea ahead of time how many times you are going to loop. # This will generate a random number from 0-99 random_number = random.randomrange(0,100) # What this is doing here is it has a condition to loop through until it hits 15. It's going to continure producing random number until It get's to 15 and as soon as it hits that it'll jump out of the loop. while(random_number != 15): print(random_number) random_number = random.randomrange(0,100) i = 0; while( i <= 22): if(i%2 == 0): print(i) elif (i == 9): break else: i += 1 # i = i + 1