本文整理汇总了Python中random.choice函数的典型用法代码示例。如果您正苦于以下问题:Python choice函数的具体用法?Python choice怎么用?Python choice使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了choice函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:eric-s-s,项目名称:dice-tables,代码行数:25,代码来源:tsting_add_speed.py
示例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
开发者ID:pydcs,项目名称:dcs,代码行数:34,代码来源:nevada_random_mission.py
示例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
开发者ID:paulsef,项目名称:to_share,代码行数:35,代码来源:multi_armed_bandit.py
示例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
开发者ID:danafallon,项目名称:Markov-twitterbot,代码行数:30,代码来源:markovgen.py
示例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)
开发者ID:paniczklos,项目名称:p2pool-right,代码行数:30,代码来源:node.py
示例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
开发者ID:mdsmus,项目名称:aristoxenus,代码行数:7,代码来源:stress.py
示例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)
开发者ID:Dogeek,项目名称:py-dnd-npc,代码行数:25,代码来源:launch.py
示例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()
开发者ID:zlobspb,项目名称:txtarantool,代码行数:32,代码来源:test_commands.py
示例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))
开发者ID:brettbonar,项目名称:CellularSelfOrganization,代码行数:34,代码来源:simulation.py
示例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
开发者ID:ShivamNegi,项目名称:Daily_Programmer,代码行数:7,代码来源:towerofascii.py
示例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()
开发者ID:PhilSk,项目名称:src,代码行数:28,代码来源:data_filling.py
示例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
开发者ID:rbessick5,项目名称:PrairieLearn,代码行数:26,代码来源:server.py
示例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])
开发者ID:rolando-contribute,项目名称:aile,代码行数:30,代码来源:meme.py
示例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()
开发者ID:2uller,项目名称:LotF,代码行数:25,代码来源:test_dumbdbm.py
示例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()
开发者ID:zlobspb,项目名称:txtarantool,代码行数:26,代码来源:test_commands.py
示例16: encode
def encode(f):
var_name = ''.join(
random.choice(string.ascii_lowercase + string.ascii_uppercase)
for i in range(50))
if _version is 2:
rev_data = binascii.b2a_base64(f)[-2::-1]
data = var_name + ' = "' + str(rev_data) + '"'
if _version is 3:
rev_data = binascii.b2a_base64(f.encode('utf8')).decode('utf8')[-2::-1]
data = var_name + ' = "' + str(rev_data) + '"'
func_name = ''.join(
random.choice(string.ascii_lowercase + string.ascii_uppercase)
for i in range(50))
func_argv = ''.join(
random.choice(string.ascii_lowercase + string.ascii_uppercase)
for i in range(50))
f = '''
import binascii
import sys
%s
def %s(%s):
if sys.version_info.major is 2:
return str(binascii.a2b_base64(%s[::-1]))
elif sys.version_info.major is 3:
return str(binascii.a2b_base64(%s[::-1]).encode('utf8'))[::-1].decode('utf8')
else:
sys.exit('Your python version is not supported!')
exec(%s(%s))
''' % (data, func_name, func_argv, func_argv, func_argv,
func_name, var_name)
return f
开发者ID:zscproject,项目名称:OWASP-ZSC,代码行数:32,代码来源:simple_base64_rev.py
示例17: test_call
def test_call(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.call("box.insert", tuple(type(x) for x in t), str(space_no0), *t)
self.assertEqual(len(r), 1)
self.assertEqual(t, r[0])
for t in data:
r = yield db.call("box.select", tuple(type(x) for x in t), str(space_no0), "0", t[0])
self.assertEqual(len(r), 1)
self.assertEqual(t, r[0])
yield db.disconnect()
开发者ID:zlobspb,项目名称:txtarantool,代码行数:31,代码来源:test_commands.py
示例18: ss
def ss():
families = []
mapping = {}
with open('easy-248.txt') as f:
for line in f.readlines():
families.append(line.split())
families.sort(key = lambda x: -len(x))
recip = []
for family in families:
recip.append(family[:])
for (i, family) in enumerate(families):
for person in family:
# select a family
other_families = list(range(len(recip)))
other_families.remove(i)
random_family = random.choice(other_families)
fail_count = 0
while len(recip[random_family]) == 0:
if fail_count > len(families):
return {}
random_family = random.choice(other_families)
# select a member within that family
random_member = random.choice(recip[random_family])
mapping[person] = random_member
# remove from recip list
recip[random_family].remove(random_member)
return mapping
开发者ID:benosment,项目名称:daily-exercises,代码行数:31,代码来源:easy-248.py
示例19: get_npc_belongings
def get_npc_belongings(npc_dict, level, npc_class):
level = int(level)
npc_belongings_value = int(level) * 100 * rd.gauss(1,0.5)
weapons = []
armors = []
belongings = ""
try:
assert npc_belongings_value >= 0
for i in range(int(level / 5) + 1):
choose_weapon = rd.choice(npc_dict["weapon"])
choose_armor = rd.choice(npc_dict["armor"])
weapons.append(choose_weapon[0])
armors.append(choose_armor[0])
npc_belongings_value -= (int(choose_weapon[1]) + int(choose_armor[1]))
except AssertionError:
for armor in armors:
belongings += armor + ", "
belongings = belongings[:-2] + "\n"
for weapon in weapons:
belongings += weapon + ", "
return belongings
for armor in armors:
belongings += armor + ", "
belongings = belongings[:-2] + "\n"
for weapon in weapons:
belongings += weapon + ", "
belongings = belongings[:-2] + "\n"
while npc_belongings_value > 0:
chosen_item = rd.choice(npc_dict["other"])
belongings += chosen_item[0] + ", "
npc_belongings_value -= int(chosen_item[1])
belongings = belongings[:-2] + "\n"
return get_npc_ac(armors, npc_class), belongings
开发者ID:Dogeek,项目名称:py-dnd-npc,代码行数:35,代码来源:launch.py
示例20: next_task
def next_task(self, channel):
if self.state == TaskManager.START:
self.map_iter = iter(self.datasource)
self.working_maps = {}
self.map_results = {}
#self.waiting_for_maps = []
self.state = TaskManager.MAPPING
if self.state == TaskManager.MAPPING:
try:
map_key = self.map_iter.next()
map_item = map_key, self.datasource[map_key]
self.working_maps[map_item[0]] = map_item[1]
return ('map', map_item)
except StopIteration:
if len(self.working_maps) > 0:
key = random.choice(self.working_maps.keys())
return ('map', (key, self.working_maps[key]))
self.state = TaskManager.REDUCING
self.reduce_iter = self.map_results.iteritems()
self.working_reduces = {}
self.results = {}
if self.state == TaskManager.REDUCING:
try:
reduce_item = self.reduce_iter.next()
self.working_reduces[reduce_item[0]] = reduce_item[1]
return ('reduce', reduce_item)
except StopIteration:
if len(self.working_reduces) > 0:
key = random.choice(self.working_reduces.keys())
return ('reduce', (key, self.working_reduces[key]))
self.state = TaskManager.FINISHED
if self.state == TaskManager.FINISHED:
self.server.handle_close()
return ('disconnect', None)
开发者ID:Fangang,项目名称:mincemeatpy,代码行数:34,代码来源:mincemeat.py
注:本文中的random.choice函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论