本文整理汇总了Python中random.choice函数的典型用法代码示例。如果您正苦于以下问题:Python choice函数的具体用法?Python choice怎么用?Python choice使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了choice函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_generator
def random_generator(num_vals, n_points_only=False):
"""outputs a random generator of len(lst) = num_vals"""
start_list = random_list(num_vals)
if n_points_only:
gen_str = "n_points"
else:
gen_str = random.choice(["one_point", "random_point", "n_points"])
if gen_str == "one_point":
loc = random.choice(["start", "mid", "end"])
generator = gen_one_point(start_list, loc)
out_str = "%s\n%s at %s" % (start_list, gen_str, loc)
elif gen_str == "random_point":
generator = gen_random_point(start_list)
out_str = "%s\n%s" % (start_list, gen_str)
else:
min_pts = len(start_list) // 10
max_pts = len(start_list) // 5
if min_pts < 1:
min_pts = 1
if max_pts < 2:
max_pts = 2
num_points = random.randrange(min_pts, max_pts + 1)
generator = gen_n_points(start_list, num_points)
out_str = "%s\n%s n=%s" % (start_list, gen_str, num_points)
return generator, out_str
示例2: heli_transport_flight
def heli_transport_flight(countries, airports: List[dcs.terrain.Airport]):
country_str = countries[random.randrange(0, len(countries))]
country = self.m.country(country_str)
transports = [x for x in country.helicopters
if x.task_default == dcs.task.Transport]
htype = random.choice(transports)
start_airport = random.choice(airports)
rand = random.random()
name = "Helicopter Transport " + str(c_count)
if 0.7 < rand:
bound = dcs.mapping.Rectangle.from_point(start_airport.position, 100*1000)
pos = bound.random_point()
hg = self.m.flight_group_inflight(country, name, htype, pos, random.randrange(800, 1500, 100), 200)
hg.add_runway_waypoint(start_airport)
hg.land_at(start_airport)
elif 0.4 < rand < 0.7:
hg = self.m.flight_group_from_airport(country, name, htype, start_airport)
hg.uncontrolled = True
else:
dest_airport = None
while True:
dest_airport = airports[random.randrange(0, len(airports))]
if dest_airport != start_airport:
break
hg = self.m.flight_group_from_airport(
country, name, htype, start_airport, start_type=random.choice(list(dcs.mission.StartType))
)
hg.add_runway_waypoint(start_airport)
hg.add_runway_waypoint(dest_airport)
hg.land_at(dest_airport)
return hg
示例3: choose
def choose(buttons, data, algorithm, threshold = .10):
"""
takes as input an iterable of button types and input data. Currently the data will be passed in the form of
a dictionary with button names as keys and boolean lists from a bernoulli distribution as
values. Optionally, set the threshold for the espilon first algorithm.
For ucb1 returns a list of length three with name, best_true_mean, and confidence_bound.
For epsilon first returns a list of length two with name and current expected mean
"""
# Exploration
rand = random()
if (rand < threshold) and (algorithm != 'ucb1'):
# if we decided to explore, choose a button at random
r_choice = choice(buttons)
# determing the reward for the choice and update reward
r_choice.reward_count += choice(data[r_choice.name])
r_choice.trial_count += 1
return [r_choice, r_choice.reward_count/r_choice.trial_count]
# if we're not in ucb1 and we're not exploring, find the max expected mean
expected_list = []
for i in range(len(buttons)):
if algorithm == 'ucb1':
confidence_bound = math.sqrt(2*math.log(buttons[i].big_n)/buttons[i].trial_count)
best_true_mean = (buttons[i].reward_count/buttons[i].trial_count) + confidence_bound
# update the expected list
expected_list.append([buttons[i], best_true_mean, confidence_bound])
buttons[i].big_n += 1
#print buttons[i], buttons[i].big_n, buttons[i].name
else:
# calculate expected mean and update to expected list
expected_list.append([buttons[i],buttons[i].reward_count/buttons[i].trial_count])
# get maximum expected value (adjusted with conf. bound for ucb1)
winner = max(expected_list, key = lambda x: x[1])
# update the reward and trial counts
winner[0].get_reward(choice(data[winner[0].name]))
return winner
示例4: make_text
def make_text(self, dictionary):
"""Takes dictionary of markov chains; returns random text."""
capital_keys_list = [key for key in dictionary.keys() if key[0][0].isupper()] # create list of only keys that start with an uppercase letter
starting_key = random.choice(capital_keys_list) # choose key (tuple) to start at
new_text_string = " ".join(starting_key) # add items in that tuple to string of created text
limit = 140
punctuation = "?.!" # create punctuation string
while dictionary.get(starting_key) != None and len(new_text_string) < limit: # Continue until the key is not found in the dictionary or until the limit is hit
value_list = dictionary[starting_key] # assign value of key (list)
next_word = random.choice(value_list) # choose random word w/in list
new_text_string = new_text_string + " " + next_word # add next_word to list of created text
starting_key = tuple(list(starting_key[1:]) + [next_word]) # create new tuple from second word of previous tuple + item at that index
#print "at start of while loop:", new_text_string
while len(new_text_string) > limit: # if length of the current string is greater than the limit, iterate through each character from the end to find punctuation
# print "if block initiated", len(new_text_string), new_text_string
for i in range(len(new_text_string)-2,-1,-1):
# print "checking next character", len(new_text_string)
if new_text_string[i] in punctuation:
new_text_string = new_text_string[0:(i+1)] # cut off string at punctuation
print "after cutting at punct:", len(new_text_string)
if len(new_text_string) <= limit:
return new_text_string
else: # if no punctuation was found:
new_text_string = new_text_string[:limit] # chop off after 140 characters
return new_text_string # return new text
示例5: download_shares
def download_shares():
while True:
desired = yield self.node.desired_var.get_when_satisfies(lambda val: len(val) != 0)
peer_addr, share_hash = random.choice(desired)
if len(self.peers) == 0:
yield deferral.sleep(1)
continue
peer = random.choice(self.peers.values())
print 'Requesting parent share %s from %s' % (p2pool_data.format_hash(share_hash), '%s:%i' % peer.addr)
try:
shares = yield peer.get_shares(
hashes=[share_hash],
parents=random.randrange(500), # randomize parents so that we eventually get past a too large block of shares
stops=list(set(self.node.tracker.heads) | set(
self.node.tracker.get_nth_parent_hash(head, min(max(0, self.node.tracker.get_height_and_last(head)[0] - 1), 10)) for head in self.node.tracker.heads
))[:100],
)
except defer.TimeoutError:
print 'Share request timed out!'
continue
except:
log.err(None, 'in download_shares:')
continue
if not shares:
yield deferral.sleep(1) # sleep so we don't keep rerequesting the same share nobody has
continue
self.handle_shares([(share, []) for share in shares], peer)
示例6: random_note
def random_note():
note = random.choice("abcdefg")
acc = random.choice(["", "-", "--", "#", "##"])
articulation = random.choice("n/otTS$RuHh;QpU[]_(){}\'s\\`~^vz,mwMW")
duration = random.choice(["2", "4", "8", "16", "32", "64"])
dot = random.choice(["", ".", ".."])
return duration + dot + note + acc + articulation
示例7: get_npc_name
def get_npc_name(npc_dict, race, gender):
race = race.lower()
gender = gender.lower()
names = npc_dict["{}_names".format(race)]
preffixes = []
intermediate = []
suffixes = []
if gender is "male" or gender is "m":
gender = "m"
else:
gender = "f"
for value_lists in names:
if value_lists[0] == "{}pre".format(gender):
preffixes.append(value_lists[1])
else:
preffixes.append(value_lists[1])
if "{}suf".format(gender) == value_lists[0]:
suffixes.append(value_lists[1])
else:
suffixes.append(value_lists[1])
if "{}in".format(gender) == value_lists[0]:
intermediate.append(value_lists[1])
else:
intermediate.append("")
return rd.choice(preffixes) + rd.choice(intermediate) + rd.choice(suffixes)
示例8: test_insert_list
def test_insert_list(self):
db = yield tnt.Connection(tnt_host, tnt_port, reconnect=False)
data = (
(
''.join(choice(insert_string_choice) for x in xrange(randint(0, insert_string_length_max))),
randint(0, 2 ** 32 - 1),
long(randint(0, 2 ** 64 - 1)),
),
(
randint(0, 2 ** 32 - 1),
''.join(choice(insert_string_choice) for x in xrange(randint(0, insert_string_length_max))),
long(randint(0, 2 ** 64 - 1)),
),
(
long(randint(0, 2 ** 64 - 1)),
randint(0, 2 ** 32 - 1),
''.join(choice(insert_string_choice) for x in xrange(randint(0, insert_string_length_max))),
),
)
defer_list = []
for t in data:
r = db.insert(space_no0, *t)
defer_list.append(r)
result = yield defer.DeferredList(defer_list)
for status, r in result:
self.assertTrue(status)
self.assertEqual(len(r), 0)
self.assertIn(" inserted", repr(r))
yield db.disconnect()
示例9: addChemotaxis
def addChemotaxis(CompuCell3DElmnt, config):
PluginElmnt_4=CompuCell3DElmnt.ElementCC3D("Plugin",{"Name":"Chemotaxis"})
# Select chemical field?
ChemicalFieldElmnt=PluginElmnt_4.ElementCC3D("ChemicalField",{"Name":"FDS","Source":"DiffusionSolverFE"})
# Define chemotaxis for cell type
# TODO: iterate over cell types
addedChemo = False
for cell in range(1, config["numCellTypes"] + 1):
if random.choice([True, False]):
addedChemo = True
ChemicalFieldElmnt.ElementCC3D("ChemotaxisByType",{"Lambda":getChemotaxisLambda(),"Type":str(cell)})
elif cell == config["numCellTypes"] and addedChemo == False:
# Always add chemotaxis for at least one cell type
ChemicalFieldElmnt.ElementCC3D("ChemotaxisByType",{"Lambda":getChemotaxisLambda(),"Type":"1"})
# Define chemical field
SteppableElmnt=CompuCell3DElmnt.ElementCC3D("Steppable",{"Type":"DiffusionSolverFE"})
DiffusionFieldElmnt=SteppableElmnt.ElementCC3D("DiffusionField",{"Name":"FDS"})
DiffusionDataElmnt=DiffusionFieldElmnt.ElementCC3D("DiffusionData")
DiffusionDataElmnt.ElementCC3D("FieldName",{},"FDS")
DiffusionDataElmnt.ElementCC3D("GlobalDiffusionConstant",{},randomFloatStr(0, 1.0))
DiffusionDataElmnt.ElementCC3D("GlobalDecayConstant",{},randomFloatStr(0, 1.0))
secretionDataElement = DiffusionFieldElmnt.ElementCC3D("SecretionData")
addedSecretion = False
for cell in range(1, config["numCellTypes"] + 1):
if random.choice([True, False]):
addedSecretion = True
secretionDataElement.ElementCC3D("Secretion", {"Type":str(cell)}, randomFloatStr(0.0, 100))
elif cell == config["numCellTypes"] and addedSecretion == False:
# Always add secretion for at least one cell type
secretionDataElement.ElementCC3D("Secretion", {"Type":"1"}, randomFloatStr(0.0, 100))
示例10: window_seg
def window_seg(floors, is_door=True):
"""Randomly generated window segment of tower.
"""
windowed = BEAM + (random.choice([' ', WINDOW]) if is_door else HI_BEAM)
for _ in range(1, floors):
windowed += BLANK + random.choice([' ', WINDOW])
return windowed + BEAM
示例11: handle
def handle(self, *args, **options):
users = list(User.objects.all())
for i in range(10):
t = Topic()
t.name = u'Topic Name {}'.format(i)
t.description = u'Topic Description {}'.format(i)
t.save()
topics = list(Topic.objects.all())
for j in range(100):
q = Question()
q.author = random.choice(users)
q.title = u'title {}'.format(j)
q.text = u'text {}'.format(j)
q.pub_date = datetime.datetime.now()
q.is_published = True
q.save()
q.topics = random.sample(topics, random.randint(1, 6))
questions = list(Question.objects.all())
for k in range(100):
c = Comment()
c.author = random.choice(users)
c.question = random.choice(questions)
c.text = u'text {}'.format(k)
c.pub_date = datetime.datetime.now()
c.save()
示例12: generate
def generate(data):
m = random.choice([3, 1.4, 1.6, 1.8])
h = random.choice([4, 12, 14, 16])
d = 1.5*h
g = 9.8
v0xmin = d*math.sqrt(g/(2*h))
v0x = round(random.choice([4, v0xmin*1.4, v0xmin*1.6, v0xmin*1.8]), 3)
data["params"]["m"] = m
data["params"]["h"] = h
data["params"]["d"] = d
data["params"]["v0x"] = v0x
t = d/v0x
data["params"]["t_c"] = round(t, 3)
data["params"]["t_x1"] = round(math.sqrt(2*h/g), 3)
data["params"]["t_x2"] = round(v0x*2/g, 3)
v0y = 0.5*g*t - h/t
data["params"]["vy_c"] = round(v0y, 2)
data["params"]["vy_x1"] = round(-math.sqrt((g*t)**2 + v0x**2/2), 2)
data["params"]["vy_x2"] = round( -0.5*g*t - h/2, 2)
data["params"]["vy_x3"] = round(-math.sqrt(v0x**2 + v0y**2), 2)
data["params"]["vy_x4"] = 0
示例13: __init__
def __init__(self, alphabet=['A', 'B', 'C'], motifs=['ABC'], m=0.2, size=100):
"""Generate a new random sequence.
Parameters:
- alphabet: a list of letters
- motifs : a list of strings
- m : probability of starting a motif at a random position
of the sequence
- size : length of the sequence
"""
self.alphabet = set(alphabet)
for motif in motifs:
self.alphabet = self.alphabet.union(motif)
self.alphabet = list(self.alphabet)
self.motifs = motifs
# Motif average length
L = sum(len(motif) for motif in motifs)/float(len(motifs))
p = m*L # threshold to insert a new motif
s = '' # random string
n = 0 # string length
while n < size:
motif = random.choice(motifs)
w = len(motif)
if random.random() <= p:
s += motif
else:
for i in xrange(w):
s += random.choice(self.alphabet)
n += w
super(TestSequence, self).__init__(s[:size])
示例14: test_random
def test_random(self):
import random
d = {} # mirror the database
for dummy in range(5):
f = dumbdbm.open(_fname)
for dummy in range(100):
k = random.choice('abcdefghijklm')
if random.random() < 0.2:
if k in d:
del d[k]
del f[k]
else:
v = random.choice('abc') * random.randrange(10000)
d[k] = v
f[k] = v
self.assertEqual(f[k], v)
f.close()
f = dumbdbm.open(_fname)
expected = d.items()
expected.sort()
got = f.items()
got.sort()
self.assertEqual(expected, got)
f.close()
示例15: test_insert
def test_insert(self):
db = yield tnt.Connection(tnt_host, tnt_port, reconnect=False)
data = (
(
''.join(choice(insert_string_choice) for x in xrange(randint(0, insert_string_length_max))),
randint(0, 2 ** 32 - 1),
long(randint(0, 2 ** 64 - 1)),
),
(
randint(0, 2 ** 32 - 1),
''.join(choice(insert_string_choice) for x in xrange(randint(0, insert_string_length_max))),
long(randint(0, 2 ** 64 - 1)),
),
(
long(randint(0, 2 ** 64 - 1)),
randint(0, 2 ** 32 - 1),
''.join(choice(insert_string_choice) for x in xrange(randint(0, insert_string_length_max))),
),
)
for t in data:
r = yield db.insert(space_no0, *t)
self.assertEqual(len(r), 0)
self.assertIn(" inserted", repr(r))
yield db.disconnect()