本文整理汇总了Python中random.rr函数的典型用法代码示例。如果您正苦于以下问题:Python rr函数的具体用法?Python rr怎么用?Python rr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了rr函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: get_random_bytes
def get_random_bytes(lrange=None, skip=None):
if lrange is None:
v = bytearray(rr(2**rr(10)))
else:
v = bytearray(rr(*lrange))
for i in range(len(v)):
v[i] = rr(256)
while v[i] == skip:
v[i] = rr(256)
return bytes(v)
开发者ID:wizardofozzie,项目名称:pylibscrypt,代码行数:10,代码来源:fuzz.py
示例2: run
def run(n):
ans = [rr(0, 2) for _ in range(n)]
x_0 = [rr(0, 2) for _ in range(n)]
if x_0 == ans:
return 1
iterations = 1
x_1 = [1 - x_i for x_i in x_0]
while x_1 != ans:
r = random()
distance = min([i for i in range(1, n) if prob[i] >= r])
x_1 = mutate(x_0, distance)
iterations += 1
return iterations
开发者ID:AntipovDen,项目名称:Master,代码行数:14,代码来源:Needle.py
示例3: bogo_sort
def bogo_sort(items):
num_items = len(items)
# Cheating :)
correct = sorted(items)
while correct != items:
rand_swap_a = rr(0, num_items)
rand_swap_b = rr(0, num_items)
if items[rand_swap_a] > items[rand_swap_b]:
copy_a = items[rand_swap_a]
copy_b = items[rand_swap_b]
items[rand_swap_a] = copy_b
items[rand_swap_b] = copy_a
print(items)
return items
开发者ID:DivyaShanmugam,项目名称:MoAL,代码行数:14,代码来源:bogo_sort.py
示例4: madlib
def madlib():
d={
'pn':['the girl','she','it'],
'v':['walking','gliding','flying'],
'pl':['dark alley','basement','subway tracks'],
'adj':['fat','sweaty','ugly'],
'noun':['boy','monster','thing']
}
return render_template(
"madlib.html",
pn =d['pn'] [rr(0, len(d['pn']) ) ],
v =d['v'] [rr(0, len(d['v']) ) ],
pl =d['pl'] [rr(0, len(d['pl']) ) ],
adj =d['adj'] [rr(0, len(d['adj']) ) ],
noun=d['noun'][rr(0, len(d['noun']) ) ]
)
开发者ID:stuycs-softdev-fall-2013,项目名称:submissions,代码行数:16,代码来源:app.py
示例5: people
def people(param_num):
people = []
# Create people
for ii in range(PEOPLE):
new_person = Person(PSHARE[param_num], PCREATE[param_num], DOI, param_num)
new_person.reset() # Init
people.append(new_person)
# Create quotas
quotas = []
for ii, topic in enumerate(TOPICS):
quotas.append(int(math.ceil(PEOPLE * I_CONF[ii])))
# Distribute interests
for ii, quota in enumerate(quotas):
for nn in range(quota):
# Interested or very interested
d = DOI[0] if rr() < DOI_PREF else DOI[1]
# Pick a suitable person. Pick any person after 3 tries.
person = 0 # Init
for jj in range(3): # HARDCODE 3
person = ri(0, PEOPLE - 1)
# Count previus topics of interest
num = 0
for interest in people[person].interests.values():
if interest != 0:
num += 1
# Give new interest if few previous interests
if num < 2: # HARDCODE 2
people[person].interests[TOPICS[ii]] = d
break
# Smaller chance to get many interests
elif rr() < 0.3: # HARDCODE 0.3
people[person].interests[TOPICS[ii]] = d
break
# Anyone goes
else:
person = ri(0, PEOPLE -1)
people[person].interests[TOPICS[ii]] = d
# Split topic
if (ii == 0):
# Preferences go half and half
people[person].preference = 1 if nn % 2 == 0 else 2
return people
开发者ID:4llu,项目名称:TuKoKe,代码行数:47,代码来源:generate.py
示例6: experiment
def experiment(N):
cnt = [0 for i in xrange(365)]
for i in xrange(N):
cnt[rr(365)] += 1
for x in cnt:
if x > 1:
return 1
return 0
开发者ID:asdoc,项目名称:l3cube_disrupt,代码行数:8,代码来源:paradox.py
示例7: MakeGraph
def MakeGraph(self, RandomGraph = True,UserTable = True):
if RandomGraph:
self.Random_Matrix = [
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0]
]
for _ in range(2):
dir = [0,1]
dir.shuffle()
while True:
x = rr(1,5)
y = rr(1,5)
if self.Random_Matrix[x][y] == self.Random_Matrix[x+dir[0]][y+dir[1]] == 0:
break
self.Random_Matrix[x][y] = rr(1,3)
if self.Random_Matrix[x][y] == 1:
self.Random_Matrix[x+dir[0]][y+dir[1]] = 2
else:
self.Random_Matrix[x+dir[0]][y+dir[1]] = 1
for _ in range(2):
while True:
x = rr(1,5)
y = rr(1,5)
if self.Random_Matrix[x][y] == self.Random_Matrix[x][y+1] == \
self.Random_Matrix[x+1][y] == self.Random_Matrix[x+1][y+1] == 0:
break
self.Random_Matrix[x][y] = self.Random_Matrix[x+1][y+1] = 1
self.Random_Matrix[x][y+1] = self.Random_Matrix[x+1][y] = 2
self.User_Matrix=[
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0]
]
开发者ID:pse1202,项目名称:NemoSemo-Reasoning-Game,代码行数:46,代码来源:GraphMaking.py
示例8: random_timesequence
def random_timesequence(start, end, steps=3):
seq = []
for n in range(start, end):
# Randomize the number of sub-steps,
# but maintain the bounds and monotonicity
# (e.g. 0, 0, 1, 1, 1, 2, 3, 3, 3)
for i in range(rr(0, steps)):
seq.append(n)
return seq
开发者ID:Androbos,项目名称:MoAL,代码行数:9,代码来源:dynamic_timewarping.py
示例9: rationals
def rationals(max_nums):
"""Doesn't cover entirety of the rationals, just an example subset"""
for n in range(0, max_nums):
try:
rand_example = rr(0, max_nums ** n) # Make things interesting
yield float(n) / float(rand_example), n, Fraction(n, rand_example)
except ZeroDivisionError:
yield 0,
raise StopIteration
开发者ID:Androbos,项目名称:MoAL,代码行数:9,代码来源:basic.py
示例10: make_person
def make_person():
return {
'name': faker.name(),
'email': faker.email(),
'address': faker.address(),
'url': faker.url(),
'created': dt.now(),
'age': rr(1, 99)
}
开发者ID:Androbos,项目名称:MoAL,代码行数:9,代码来源:csv_test.py
示例11: emit
def emit(routing_key='info'):
count = 0
# Randomize to mimic "authenticity"
time.sleep(float(rr(1, 10)) * 0.1)
start.channel.basic_publish(
exchange=start.EXCHANGE_NAME,
routing_key=routing_key,
body='MOAL Message! #{}'.format(count),
properties=pika.BasicProperties(delivery_mode=2))
count += 1
开发者ID:Androbos,项目名称:MoAL,代码行数:10,代码来源:emitter.py
示例12: compute
def compute(self, recipe=1):
"""Don't be confused: the notion of a recipe is just for fun here.
It has nothing to do with lightmaps -- it's just to experiment
with example 'ascii lighting' for more interesting visualizations.
"""
self.lighting = []
pixels = self.height * self.width
if recipe == 1:
for k in xrange(pixels):
self.lighting.append(rr(0, 255))
elif recipe == 2:
for k in xrange(pixels):
self.lighting.append(rr(0, (k + 1) * 5))
elif recipe == 3:
for k in xrange(pixels):
if self.width < self.height:
self.lighting.append(rr(self.width, self.height))
else:
self.lighting.append(rr(self.height, self.width))
开发者ID:Androbos,项目名称:MoAL,代码行数:19,代码来源:lightmap.py
示例13: friends
def friends(people, param_num):
for ii, person in enumerate(people):
# Send friend requests
for nn in range(int(gauss(F_MEAN[param_num], F_DEVIATION[param_num]))):
# Pick a person
id = ri(0, PEOPLE - 1)
# Skip self
if id == ii:
continue
other_person = people[id]
# Logic of accepting a friend request
# Amount of previous friends
# HARDCODE All numbers
if len(other_person.friends) < 8 or (len(other_person.friends) < 12 and rr() < 0.7) or rr() < 0.3:
prob = 0.6 - F_PROB_BONUS[param_num]
# Mutual interests
for jj, topic in enumerate(TOPICS):
# Split topic
if jj == 0:
# No preference
if other_person.preference == 0 or person.preference == 0:
pass
# Same preference
elif person.preference == other_person.preference:
# Shared interest
prob += F_PROB_BONUS[param_num]
# Bonus
prob += 0.1
# Different preference
else:
# Penalty
prob -= 0.1
# Rest
else:
# Shared interest
if other_person.interests[topic] > 0 and person.interests[topic] > 0:
prob += F_PROB_BONUS[param_num]
# Accept of decline friend request
if rr() < prob:
person.friends.append(id)
other_person.friends.append(ii)
开发者ID:4llu,项目名称:TuKoKe,代码行数:42,代码来源:generate.py
示例14: _divvy_ram
def _divvy_ram(self, ram):
"""This is an arbitrary, somewhat meaningless method, to simulate
the notion of free blocks of ram that must be allocated and
referenced via pointer. A given program may require X ram, but
the location of available ram may be disparate, so various blocks have
to be stored as a free linked list, or similar data structure.
Since we've already covered linked lists, association lists, etc...,
there isn't much value outside learning the context of this data
structure, which tends to be in memory management."""
subdivisions = rr(2, 20) # Arbitrary number
flist = []
while subdivisions > 0:
diff = rr(0, ram)
# Store ram as [current block of memory, location]
flist.append([diff, str(self.last_block).zfill(4)])
ram = ram - diff
self.last_block += 1
subdivisions -= 1
if DEBUG:
print_info('Ram: {} / diff: {}'.format(ram, diff))
return flist
开发者ID:Androbos,项目名称:MoAL,代码行数:21,代码来源:free_list.py
示例15: make_person
def make_person(omit=[]):
val = {
'name': faker.name(),
'address': faker.address(),
'url': faker.url(),
'is_married': choice([True, False]),
'created': '{}'.format(dt.now()),
'age': rr(1, 99)
}
for _val in omit:
if _val in val:
val.pop(_val)
return val
开发者ID:Androbos,项目名称:MoAL,代码行数:13,代码来源:json_test.py
示例16: create
def create(self):
for topic in self.interests:
# Check if interested
if self.interests[topic] != 0:
# To create or not to create
if rr() < self.pCreate:
# Create
for ii in range(ri(0, 3)): # HARDCODE (0, 3)
# Add preference if necessary
pref = 0
if topic == TOPICS[0]:
pref = self.preference
self.outbox.append(Post(topic, 1, pref))
开发者ID:4llu,项目名称:TuKoKe,代码行数:13,代码来源:person.py
示例17: share
def share(self):
for post in self.buffer:
# Probability of sharing and skip posts of opposite preference
if (
rr() < self.pShare
and (post.preference == 0 or post.preference == self.preference)
and self.interests[post.topic] != 0
):
# Create new post for sharing
pref = 0
# Add preference if necessary
if post.topic == TOPICS[0]:
pref = self.preference
self.outbox.append(Post(post.topic, 2, pref))
开发者ID:4llu,项目名称:TuKoKe,代码行数:14,代码来源:person.py
示例18: mkxword
def mkxword(self):
l = [k for k in self.d]
counter = 0
if self.words is None:
self.words = []
across = 0
while True:
counter += 1
w = Ch(l)
m, n = rr(16), rr(16)
if len(self.words) == 24:
print('correct exit point, hardwired n of 24', len(self.words))
return
elif across:
if self.addwordacross(w, m, n) is True:
l.pop(l.index(w))
self.words.append((w, m, n, across))
#tuple format (0 word, 1 m, 2 n, 3 across<bool> )
across ^= 1
elif across == 0:
if self.addworddown(w, m, n) is True:
l.pop(l.index(w))
self.words.append((w, m, n, across))
across ^= 1
elif counter > 55:
print('self.words', self.words)
print('len(self.words)', len(self.words), 'type', type(self.words))
print('counter', counter)
ridx = rr(len(self.words))
word, idxm, idxn, across = self.words.pop(ridx)
self.rmword(word, idxm, idxn, across)
print('counter reset. removed word', word)
counter = 0
continue
开发者ID:NSMobileCS,项目名称:xword,代码行数:37,代码来源:xword.py
示例19: plotpolar
def plotpolar(data=[], num=None):
"""data and num used to build a graph using matplotlib,
data[0] is title
data[1] is which_quarter
data[2] is organization_name
data[3] is the list of radii
"""
fig = figure(figsize=(10,10))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
#ax = gca()
#ax.set_autoscale_on(False)
#xmin,xmax,ymin,ymax
ax.set_ylim(0,10)
deg = [360/len(organization_labels) * x for x in range(1,len(organization_labels)+1)]
theta = [i*pi/180 for i in deg] # convert to radians
#FIXME when possible reflect user input, atm random # of graph bars
# random..
if not data:
radii = [rr(0,11) for x in range(len(organization_labels)-1)]
radii.append(num)
else:
title_ext = data[0] #leave blank for no title
which_quarter = data[1]
organization_name = data[2]
radii = data[3]
#TODO creates graph bars
bars = ax.bar(theta,radii, width=0.35, bottom=0.0, align='center')
for r,bar in zip(radii, bars):
bar.set_facecolor( cm.jet(r/10.))
bar.set_alpha(0.5)
#degree organization_labels
ax.set_thetagrids(deg,organization_labels, frac= 1, fontsize=14, verticalalignment = 'top',weight ="bold", color = "blue",clip_on =True)
#title
ax.set_title(title_ext + " " + which_quarter + " " + organization_name , fontsize=30, weight="bold")
canvas=FigureCanvas(fig)
#String.IO, i believe allows us to treat our object as if it were a file.
png_output = StringIO.StringIO()
canvas.print_png(png_output)
#how to return it to simple in views
return png_output
开发者ID:drewverlee,项目名称:petal,代码行数:49,代码来源:graph.py
示例20: filter
def filter(self, buffer, interests, preference):
ret = []
bonus = 0
for post in buffer:
var bonus = 0
if post.preference != 0:
if post.preference == preference:
bonus = 0.1
else:
bonus = -0.2
if interests[post.topic] + bonus < 0:
bonus = 0
if rr() < interests[post.topic] + bonus:
ret.append(post)
return ret
开发者ID:4llu,项目名称:TuKoKe,代码行数:15,代码来源:filter.py
注:本文中的random.rr函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论