Python random.random() Examples
The following are 30
code examples of random.random().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
random
, or try the search function
.

Example #1
Source Project: indras_net Author: gcallah File: hiv.py License: GNU General Public License v3.0 | 7 votes |
def __init__(self, name, infected, infection_length, initiative, coupling_tendency, condom_use, test_frequency, commitment, coupled=False, coupled_length=0, known=False, partner=None): init_state = random.randint(0, 3) super().__init__(name, "wandering around", NSTATES, init_state) self.coupled = coupled self.couple_length = coupled_length self.partner = partner self.initiative = initiative self.infected = infected self.known = known self.infection_length = infection_length self.coupling_tendency = coupling_tendency self.condom_use = condom_use self.test_frequency = test_frequency self.commitment = commitment self.state = init_state self.update_ntype()
Example #2
Source Project: indras_net Author: gcallah File: el_farol.py License: GNU General Public License v3.0 | 6 votes |
def discourage(unwanted): """ Discourages extra drinkers from going to the bar by decreasing motivation. Chooses drinkers randomly from the drinkers that went to the bar. """ discouraged = 0 drinkers = get_group(DRINKERS) while unwanted: if DEBUG: user_tell("The members are: " + drinkers.members) rand_name = random.choice(list(drinkers.members)) rand_agent = drinkers[rand_name] if DEBUG: user_tell("drinker ", rand_agent, " = " + repr(drinkers[rand_agent])) rand_agent[MOTIV] = max(rand_agent[MOTIV] - DISC_AMT, MIN_MOTIV) discouraged += 1 unwanted -= 1 return discouraged
Example #3
Source Project: simulated-annealing-tsp Author: chncyhn File: anneal.py License: MIT License | 6 votes |
def initial_solution(self): """ Greedy algorithm to get an initial solution (closest-neighbour). """ cur_node = random.choice(self.nodes) # start from a random node solution = [cur_node] free_nodes = set(self.nodes) free_nodes.remove(cur_node) while free_nodes: next_node = min(free_nodes, key=lambda x: self.dist(cur_node, x)) # nearest neighbour free_nodes.remove(next_node) solution.append(next_node) cur_node = next_node cur_fit = self.fitness(solution) if cur_fit < self.best_fitness: # If best found so far, update best fitness self.best_fitness = cur_fit self.best_solution = solution self.fitness_list.append(cur_fit) return solution, cur_fit
Example #4
Source Project: simulated-annealing-tsp Author: chncyhn File: anneal.py License: MIT License | 6 votes |
def anneal(self): """ Execute simulated annealing algorithm. """ # Initialize with the greedy solution. self.cur_solution, self.cur_fitness = self.initial_solution() print("Starting annealing.") while self.T >= self.stopping_temperature and self.iteration < self.stopping_iter: candidate = list(self.cur_solution) l = random.randint(2, self.N - 1) i = random.randint(0, self.N - l) candidate[i : (i + l)] = reversed(candidate[i : (i + l)]) self.accept(candidate) self.T *= self.alpha self.iteration += 1 self.fitness_list.append(self.cur_fitness) print("Best fitness obtained: ", self.best_fitness) improvement = 100 * (self.fitness_list[0] - self.best_fitness) / (self.fitness_list[0]) print(f"Improvement over greedy heuristic: {improvement : .2f}%")
Example #5
Source Project: EDeN Author: fabriziocosta File: estimator_utils.py License: MIT License | 6 votes |
def make_train_test_sets(pos_graphs, neg_graphs, test_proportion=.3, random_state=2): """make_train_test_sets.""" random.seed(random_state) random.shuffle(pos_graphs) random.shuffle(neg_graphs) pos_dim = len(pos_graphs) neg_dim = len(neg_graphs) tr_pos_graphs = pos_graphs[:-int(pos_dim * test_proportion)] te_pos_graphs = pos_graphs[-int(pos_dim * test_proportion):] tr_neg_graphs = neg_graphs[:-int(neg_dim * test_proportion)] te_neg_graphs = neg_graphs[-int(neg_dim * test_proportion):] tr_graphs = tr_pos_graphs + tr_neg_graphs te_graphs = te_pos_graphs + te_neg_graphs tr_targets = [1] * len(tr_pos_graphs) + [0] * len(tr_neg_graphs) te_targets = [1] * len(te_pos_graphs) + [0] * len(te_neg_graphs) tr_graphs, tr_targets = paired_shuffle(tr_graphs, tr_targets) te_graphs, te_targets = paired_shuffle(te_graphs, te_targets) return (tr_graphs, np.array(tr_targets)), (te_graphs, np.array(te_targets))
Example #6
Source Project: deep-siamese-text-similarity Author: dhwajraj File: input_helpers.py License: MIT License | 6 votes |
def getTsvData(self, filepath): print("Loading training data from "+filepath) x1=[] x2=[] y=[] # positive samples from file for line in open(filepath): l=line.strip().split("\t") if len(l)<2: continue if random() > 0.5: x1.append(l[0].lower()) x2.append(l[1].lower()) else: x1.append(l[1].lower()) x2.append(l[0].lower()) y.append(int(l[2])) return np.asarray(x1),np.asarray(x2),np.asarray(y)
Example #7
Source Project: deep-siamese-text-similarity Author: dhwajraj File: input_helpers.py License: MIT License | 6 votes |
def batch_iter(self, data, batch_size, num_epochs, shuffle=True): """ Generates a batch iterator for a dataset. """ data = np.asarray(data) print(data) print(data.shape) data_size = len(data) num_batches_per_epoch = int(len(data)/batch_size) + 1 for epoch in range(num_epochs): # Shuffle the data at each epoch if shuffle: shuffle_indices = np.random.permutation(np.arange(data_size)) shuffled_data = data[shuffle_indices] else: shuffled_data = data for batch_num in range(num_batches_per_epoch): start_index = batch_num * batch_size end_index = min((batch_num + 1) * batch_size, data_size) yield shuffled_data[start_index:end_index]
Example #8
Source Project: deep-siamese-text-similarity Author: dhwajraj File: train.py License: MIT License | 6 votes |
def train_step(x1_batch, x2_batch, y_batch): """ A single training step """ if random()>0.5: feed_dict = { siameseModel.input_x1: x1_batch, siameseModel.input_x2: x2_batch, siameseModel.input_y: y_batch, siameseModel.dropout_keep_prob: FLAGS.dropout_keep_prob, } else: feed_dict = { siameseModel.input_x1: x2_batch, siameseModel.input_x2: x1_batch, siameseModel.input_y: y_batch, siameseModel.dropout_keep_prob: FLAGS.dropout_keep_prob, } _, step, loss, accuracy, dist, sim, summaries = sess.run([tr_op_set, global_step, siameseModel.loss, siameseModel.accuracy, siameseModel.distance, siameseModel.temp_sim, train_summary_op], feed_dict) time_str = datetime.datetime.now().isoformat() print("TRAIN {}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy)) train_summary_writer.add_summary(summaries, step) print(y_batch, dist, sim)
Example #9
Source Project: DDPAE-video-prediction Author: jthsieh File: video_transforms.py License: MIT License | 6 votes |
def __call__(self, video): """ Args: video (np.ndarray): Video to be cropped. Returns: np.ndarray: Cropped video. """ if self.padding > 0: pad = Pad(self.padding, 0) video = pad(video) w, h = video.shape[-2], video.shape[-3] th, tw = self.size if w == tw and h == th: return video x1 = random.randint(0, w-tw) y1 = random.randint(0, h-th) return video[..., y1:y1+th, x1:x1+tw, :]
Example #10
Source Project: DDPAE-video-prediction Author: jthsieh File: video_transforms.py License: MIT License | 6 votes |
def __call__(self, video): for attempt in range(10): area = video.shape[-3]*video.shape[-2] target_area = random.uniform(0.08, 1.0)*area aspect_ratio = random.uniform(3./4, 4./3) w = int(round(math.sqrt(target_area*aspect_ratio))) h = int(round(math.sqrt(target_area/aspect_ratio))) if random.random() < 0.5: w, h = h, w if w <= video.shape[-2] and h <= video.shape[-3]: x1 = random.randint(0, video.shape[-2]-w) y1 = random.randint(0, video.shape[-3]-h) video = video[..., y1:y1+h, x1:x1+w, :] return resize(video, (self.size, self.size), self.interpolation) # Fallback scale = Scale(self.size, interpolation=self.interpolation) crop = CenterCrop(self.size) return crop(scale(video))
Example #11
Source Project: DDPAE-video-prediction Author: jthsieh File: moving_mnist.py License: MIT License | 6 votes |
def __getitem__(self, idx): length = self.n_frames_input + self.n_frames_output if self.is_train or self.num_objects[0] != 2: # Sample number of objects num_digits = random.choice(self.num_objects) # Generate data on the fly images = self.generate_moving_mnist(num_digits) else: images = self.dataset[:, idx, ...] if self.transform is not None: images = self.transform(images) input = images[:self.n_frames_input] if self.n_frames_output > 0: output = images[self.n_frames_input:length] else: output = [] return input, output
Example #12
Source Project: zmirror Author: aploium File: zmirror.py License: MIT License | 6 votes |
def generate_ip_verify_hash(input_dict): """ 生成一个标示用户身份的hash 在 human_ip_verification 功能中使用 hash一共14位 hash(前7位+salt) = 后7位 以此来进行验证 :rtype str """ strbuff = human_ip_verification_answers_hash_str for key in input_dict: strbuff += key + input_dict[key] + str(random.randint(0, 9000000)) input_key_hash = hex(zlib.adler32(strbuff.encode(encoding='utf-8')))[2:] while len(input_key_hash) < 7: input_key_hash += '0' output_hash = hex(zlib.adler32((input_key_hash + human_ip_verification_answers_hash_str).encode(encoding='utf-8')))[2:] while len(output_hash) < 7: output_hash += '0' return input_key_hash + output_hash
Example #13
Source Project: fuku-ml Author: fukuball File: Utility.py License: MIT License | 6 votes |
def random_projection(X): data_demension = X.shape[1] new_data_demension = random.randint(2, data_demension) new_X = np.empty((data_demension, new_data_demension)) minus_one = 0.1 positive_one = 0.9 for i in range(len(new_X)): for j in range(len(new_X[i])): rand = random.random() if rand < minus_one: new_X[i][j] = -1.0 elif rand >= positive_one: new_X[i][j] = 1.0 else: new_X[i][j] = 0.0 new_X = np.inner(X, new_X.T) return new_X
Example #14
Source Project: fullrmc Author: bachiraoun File: Constraint.py License: GNU Affero General Public License v3.0 | 6 votes |
def should_step_get_rejected(self, standardError): """ Given a standard error, return whether to keep or reject new standard error according to the constraint reject probability. :Parameters: #. standardError (number): The standard error to compare with the Constraint standard error :Return: #. result (boolean): True to reject step, False to accept """ if self.standardError is None: raise Exception(LOGGER.error("must compute data first")) if standardError<=self.standardError: return False return randfloat() < self.__rejectProbability
Example #15
Source Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: util.py License: Apache License 2.0 | 6 votes |
def estimate_density(DATA_PATH, feature_size): """sample 10 times of a size of 1000 for estimating the density of the sparse dataset""" if not os.path.exists(DATA_PATH): raise Exception("Data is not there!") density = [] P = 0.01 for _ in range(10): num_non_zero = 0 num_sample = 0 with open(DATA_PATH) as f: for line in f: if (random.random() < P): num_non_zero += len(line.split(" ")) - 1 num_sample += 1 density.append(num_non_zero * 1.0 / (feature_size * num_sample)) return sum(density) / len(density)
Example #16
Source Project: unicorn-hat-hd Author: pimoroni File: forest-fire.py License: MIT License | 5 votes |
def initialise(): forest = [[tree if random.random() <= initial_trees else space for x in range(forest_width)] for y in range(forest_height)] return forest
Example #17
Source Project: unicorn-hat-hd Author: pimoroni File: forest-fire.py License: MIT License | 5 votes |
def update_forest(forest): new_forest = [[space for x in range(forest_width)] for y in range(forest_height)] for x in range(forest_width): for y in range(forest_height): if forest[x][y] == burning: new_forest[x][y] = space elif forest[x][y] == space: new_forest[x][y] = tree if random.random() <= p else space elif forest[x][y] == tree: neighbours = get_neighbours(x, y, hood_size) new_forest[x][y] = (burning if any([forest[n[0]][n[1]] == burning for n in neighbours]) or random.random() <= f else tree) return new_forest
Example #18
Source Project: indras_net Author: gcallah File: agent.py License: GNU General Public License v3.0 | 5 votes |
def prob_state_trans(curr_state, states): """ Do a probabilistic state transition. """ new_state = curr_state r = random() cum_prob = 0.0 for trans_state in range(len(states[curr_state])): cum_prob += states[curr_state][trans_state] if cum_prob >= r: new_state = trans_state break return new_state
Example #19
Source Project: indras_net Author: gcallah File: agent.py License: GNU General Public License v3.0 | 5 votes |
def move(self, max_move=DEF_MAX_MOVE, angle=None): """ Move this agent to a random pos within max_move of its current pos. """ if (self.is_located() and self.locator is not None and not self.locator.is_full()): new_xy = None if angle is not None: if DEBUG2: user_log_notif("Using angled move") new_xy = self.locator.point_from_vector(angle, max_move, self.pos) self.locator.place_member(self, max_move=max_move, xy=new_xy)
Example #20
Source Project: indras_net Author: gcallah File: markov.py License: GNU General Public License v3.0 | 5 votes |
def probvec_to_state(pv): state_vec = None cum_prob = 0.0 r = random.random() l = pv.shape[COLS] for i in range(l): cum_prob += pv.item(i) if cum_prob >= r: state_vec = state_vector(l, i) break return state_vec
Example #21
Source Project: indras_net Author: gcallah File: bigbox.py License: GNU General Public License v3.0 | 5 votes |
def utils_from_good(self, good): if not self.sells(good): return NA else: return (random.random() + self.util_adj) * ADJ_SCALING_FACTOR
Example #22
Source Project: indras_net Author: gcallah File: hiv.py License: GNU General Public License v3.0 | 5 votes |
def infect(self): if (self.coupled is True and self.infected is True and self.known is False and self.partner.infected is False): if (10 * random.random() > self.condom_use or 10 * random.random() > self.partner.condom_use): if 100 * random.random() < INFECTION_CHANCE: self.partner.infected = True # print(self.name, "has infected", self.partner.name)
Example #23
Source Project: indras_net Author: gcallah File: hiv.py License: GNU General Public License v3.0 | 5 votes |
def preact(self): # print(self.name, "is preacting") self.update_ntype() if self.coupled is False: if 10 * random.random() < self.coupling_tendency: self.couple() if isinstance(self.partner, str): self.partner = self.env.get_obj(self.partner) if self.coupled is True: if isinstance(self.partner.partner, str): self.partner.partner = self.env.get_obj(self.partner.partner) self.infect() self.partner.infect()
Example #24
Source Project: indras_net Author: gcallah File: hiv.py License: GNU General Public License v3.0 | 5 votes |
def test(self): if (2 * random.random() < self.test_frequency and self.infected is True and self.known is False): self.known = True # print(self.name, "has been tested positive") if self.infection_length > SYMPTOMS_SHOW: if random.random() <= SYMPTOMS_SHOW_PROB: self.known = True # print(self.name, "has developed symptoms")
Example #25
Source Project: indras_net Author: gcallah File: coop.py License: GNU General Public License v3.0 | 5 votes |
def act(self): self.sitting = False if self.coupons < self.min_holdings: self.goal = BABYSIT elif self.coupons > self.min_holdings: if random.random() > .5: self.goal = GO_OUT else: self.goal = BABYSIT
Example #26
Source Project: indras_net Author: gcallah File: bigbox.py License: GNU General Public License v3.0 | 5 votes |
def get_rand_good_type(): """ Randomly select consumer's item needed after each run. """ return random.choice(list(mp_stores.keys()))
Example #27
Source Project: indras_net Author: gcallah File: bigbox.py License: GNU General Public License v3.0 | 5 votes |
def calc_util(stores): """ calculate utility for stores. """ return random.random()
Example #28
Source Project: indras_net Author: gcallah File: el_farol.py License: GNU General Public License v3.0 | 5 votes |
def get_decision(agent): """ Makes a decision for the agent whether or not to go to the bar """ return random.random() <= agent[MOTIV]
Example #29
Source Project: indras_net Author: gcallah File: coop.py License: GNU General Public License v3.0 | 5 votes |
def babysitter_action(agent): """ Co-op members act as follows: if their holding coupons are less than desired cash balance, they babysit, or there is a 50-50 chance for them to go out. """ if agent['coupons'] <= agent['desired_cash']: agent['goal'] = "BABYSITTING" else: if random.random() > .5: agent['goal'] = "GOING_OUT" else: agent['goal'] = "BABYSITTING" return True
Example #30
Source Project: indras_net Author: gcallah File: cap_struct.py License: GNU General Public License v3.0 | 5 votes |
def create_rholder(name, i, props=None): """ Create an agent. """ k_price = DEF_K_PRICE resources = copy.deepcopy(DEF_CAP_WANTED) num_resources = len(resources) price_list = copy.deepcopy(DEF_EACH_CAP_PRICE) if props is not None: k_price = props.get('cap_price', DEF_K_PRICE) for k in price_list.keys(): price_list[k] = float("{0:.2f}".format(float(k_price * random.uniform(0.5, 1.5)))) starting_cash = DEF_RHOLDER_CASH if props is not None: starting_cash = get_prop('rholder_starting_cash', DEF_RHOLDER_CASH) if props is not None: total_resources = get_prop('rholder_starting_resource_total', DEF_TOTAL_RESOURCES_RHOLDER_HAVE) for k in resources.keys(): resources[k] = int((total_resources * 2) * (random.random() / num_resources)) return Agent(name + str(i), action=rholder_action, attrs={"cash": starting_cash, "resources": resources, "price": price_list})