本文整理汇总了Python中random.expovariate方法的典型用法代码示例。如果您正苦于以下问题:Python random.expovariate方法的具体用法?Python random.expovariate怎么用?Python random.expovariate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类random
的用法示例。
在下文中一共展示了random.expovariate方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: poisson_source
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def poisson_source(rate, iterable, target):
"""Send events at random times with uniform probability.
Args:
rate: The average number of events to send per second.
iterable: A series of items which will be sent to the target one by one.
target: The target coroutine or sink.
Returns:
An iterator over any remaining items.
"""
if rate <= 0.0:
raise ValueError("poisson_source rate {} is not positive".format(rate))
it = iter(iterable)
for item in it:
duration = random.expovariate(rate)
sleep(duration)
try:
target.send(item)
except StopIteration:
return prepend(item, it)
return empty_iter()
示例2: test_explicit_from_float
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def test_explicit_from_float(self):
r = Decimal(0.1)
self.assertEqual(type(r), Decimal)
self.assertEqual(str(r),
'0.1000000000000000055511151231257827021181583404541015625')
self.assertTrue(Decimal(float('nan')).is_qnan())
self.assertTrue(Decimal(float('inf')).is_infinite())
self.assertTrue(Decimal(float('-inf')).is_infinite())
self.assertEqual(str(Decimal(float('nan'))),
str(Decimal('NaN')))
self.assertEqual(str(Decimal(float('inf'))),
str(Decimal('Infinity')))
self.assertEqual(str(Decimal(float('-inf'))),
str(Decimal('-Infinity')))
self.assertEqual(str(Decimal(float('-0.0'))),
str(Decimal('-0')))
for i in range(200):
x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
self.assertEqual(x, float(Decimal(x))) # roundtrip
示例3: test_from_float
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def test_from_float(self):
class MyDecimal(Decimal):
pass
r = MyDecimal.from_float(0.1)
self.assertEqual(type(r), MyDecimal)
self.assertEqual(str(r),
'0.1000000000000000055511151231257827021181583404541015625')
bigint = 12345678901234567890123456789
self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint))
self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan())
self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite())
self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite())
self.assertEqual(str(MyDecimal.from_float(float('nan'))),
str(Decimal('NaN')))
self.assertEqual(str(MyDecimal.from_float(float('inf'))),
str(Decimal('Infinity')))
self.assertEqual(str(MyDecimal.from_float(float('-inf'))),
str(Decimal('-Infinity')))
self.assertRaises(TypeError, MyDecimal.from_float, 'abc')
for i in range(200):
x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip
示例4: _fix
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def _fix(self, n=0):
o = random.choice(self.objlist)
if issubclass(o, ASN1_INTEGER):
return o(int(random.gauss(0,1000)))
elif issubclass(o, ASN1_IPADDRESS):
z = RandIP()._fix()
return o(z)
elif issubclass(o, ASN1_STRING):
z = int(random.expovariate(0.05)+1)
return o("".join([random.choice(self.chars) for i in range(z)]))
elif issubclass(o, ASN1_SEQUENCE) and (n < 10):
z = int(random.expovariate(0.08)+1)
return o(map(lambda x:x._fix(n+1), [self.__class__(objlist=self.objlist)]*z))
return ASN1_INTEGER(int(random.gauss(0,1000)))
##############
#### ASN1 ####
##############
示例5: random_tree_demo
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def random_tree_demo(num_leaf_pops, lins_per_pop):
#events_list = []
sampled_pops = list(range(1, num_leaf_pops + 1))
model = momi.DemographicModel(1.0, .25)
for p in sampled_pops:
model.add_leaf(p, N=random.expovariate(1.0))
roots = list(sampled_pops)
#for i in roots:
# events_list += [('-en', 0.0, i, random.expovariate(1.0))]
t = 0.0
while len(roots) > 1:
i, j = random.sample(roots, 2)
t += random.expovariate(1.0)
#events_list += [('-ej', t, i, j),
# ('-en', t, j, random.expovariate(1.0))]
model.move_lineages(i, j, t, N=random.expovariate(1.0))
roots.remove(i)
#return make_demo_hist(events_list, sampled_pops, [lins_per_pop] * num_leaf_pops)
return model
# return make_demography(events_list, sampled_pops, [lins_per_pop] *
# num_leaf_pops)
示例6: test_pick_providers
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def test_pick_providers(self, mock1):
self.vps = cloudomate.get_vps_providers
self.get_gateway = blueAngel.BlueAngelHost.get_gateway
self.estimate_price = Coinbase.Coinbase.estimate_price
self.get_price = wallet_util.get_price
self.get_fee = wallet_util.get_network_fee
# cloudomate.get_vps_providers = MagicMock(
# return_value=CaseInsensitiveDict({'blueangelhost': blueAngel.BlueAngelHost}))
random.expovariate = MagicMock(return_value=0.55)
blueAngel.BlueAngelHost.get_gateway = MagicMock()
Coinbase.Coinbase.estimate_price = MagicMock()
cloudomate.pick_option = MagicMock(return_value=[1, 2, 3])
wallet_util.get_price = MagicMock()
wallet_util.get_network_fee = MagicMock()
cloudomate.pick_provider(cloudomate.get_vps_providers())
blueAngel.BlueAngelHost.get_gateway.assert_called_once()
cloudomate.get_vps_providers = self.vps
blueAngel.BlueAngelHost.get_gateway = self.get_gateway
Coinbase.Coinbase.estimate_price = self.estimate_price
wallet_util.get_price = self.get_price
wallet_util.get_network_fee = self.get_fee
示例7: _fix
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def _fix(self, n=0):
o = random.choice(self.objlist)
if issubclass(o, ASN1_INTEGER):
return o(int(random.gauss(0, 1000)))
elif issubclass(o, ASN1_IPADDRESS):
z = RandIP()._fix()
return o(z)
elif issubclass(o, ASN1_GENERALIZED_TIME) or issubclass(o, ASN1_UTC_TIME): # noqa: E501
z = GeneralizedTime()._fix()
return o(z)
elif issubclass(o, ASN1_STRING):
z = int(random.expovariate(0.05) + 1)
return o("".join(random.choice(self.chars) for _ in range(z)))
elif issubclass(o, ASN1_SEQUENCE) and (n < 10):
z = int(random.expovariate(0.08) + 1)
return o([self.__class__(objlist=self.objlist)._fix(n + 1)
for _ in range(z)])
return ASN1_INTEGER(int(random.gauss(0, 1000)))
##############
# ASN1 #
##############
示例8: test_explicit_from_float
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def test_explicit_from_float(self):
Decimal = self.decimal.Decimal
r = Decimal(0.1)
self.assertEqual(type(r), Decimal)
self.assertEqual(str(r),
'0.1000000000000000055511151231257827021181583404541015625')
self.assertTrue(Decimal(float('nan')).is_qnan())
self.assertTrue(Decimal(float('inf')).is_infinite())
self.assertTrue(Decimal(float('-inf')).is_infinite())
self.assertEqual(str(Decimal(float('nan'))),
str(Decimal('NaN')))
self.assertEqual(str(Decimal(float('inf'))),
str(Decimal('Infinity')))
self.assertEqual(str(Decimal(float('-inf'))),
str(Decimal('-Infinity')))
self.assertEqual(str(Decimal(float('-0.0'))),
str(Decimal('-0')))
for i in range(200):
x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
self.assertEqual(x, float(Decimal(x))) # roundtrip
示例9: test_explicit_context_create_from_float
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def test_explicit_context_create_from_float(self):
Decimal = self.decimal.Decimal
nc = self.decimal.Context()
r = nc.create_decimal(0.1)
self.assertEqual(type(r), Decimal)
self.assertEqual(str(r), '0.1000000000000000055511151231')
self.assertTrue(nc.create_decimal(float('nan')).is_qnan())
self.assertTrue(nc.create_decimal(float('inf')).is_infinite())
self.assertTrue(nc.create_decimal(float('-inf')).is_infinite())
self.assertEqual(str(nc.create_decimal(float('nan'))),
str(nc.create_decimal('NaN')))
self.assertEqual(str(nc.create_decimal(float('inf'))),
str(nc.create_decimal('Infinity')))
self.assertEqual(str(nc.create_decimal(float('-inf'))),
str(nc.create_decimal('-Infinity')))
self.assertEqual(str(nc.create_decimal(float('-0.0'))),
str(nc.create_decimal('-0')))
nc.prec = 100
for i in range(200):
x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
self.assertEqual(x, float(nc.create_decimal(x))) # roundtrip
示例10: __init__
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def __init__(self, index_name, type_name, conflicting_ids=None, conflict_probability=None, on_conflict=None,
recency=None, rand=random.random, randint=random.randint, randexp=random.expovariate):
if type_name:
self.meta_data_index_with_id = '{"index": {"_index": "%s", "_type": "%s", "_id": "%s"}}\n' % \
(index_name, type_name, "%s")
self.meta_data_update_with_id = '{"update": {"_index": "%s", "_type": "%s", "_id": "%s"}}\n' % \
(index_name, type_name, "%s")
self.meta_data_index_no_id = '{"index": {"_index": "%s", "_type": "%s"}}\n' % (index_name, type_name)
else:
self.meta_data_index_with_id = '{"index": {"_index": "%s", "_id": "%s"}}\n' % (index_name, "%s")
self.meta_data_update_with_id = '{"update": {"_index": "%s", "_id": "%s"}}\n' % (index_name, "%s")
self.meta_data_index_no_id = '{"index": {"_index": "%s"}}\n' % index_name
self.conflicting_ids = conflicting_ids
self.on_conflict = on_conflict
# random() produces numbers between 0 and 1 and the user denotes the probability in percentage between 0 and 100
self.conflict_probability = conflict_probability / 100.0 if conflict_probability is not None else 0
self.recency = recency if recency is not None else 0
self.rand = rand
self.randint = randint
self.randexp = randexp
self.id_up_to = 0
示例11: _think
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def _think(self):
try:
if len(self.conns) < self.desired_conns and len(self.attempts) < self.max_attempts and self.node.addr_store:
(host, port), = self.node.get_good_peers(1)
if self._host_to_ident(host) in self.attempts:
pass
elif host in self.node.bans and self.node.bans[host] > time.time():
pass
else:
#print 'Trying to connect to', host, port
reactor.connectTCP(host, port, self, timeout=5)
except:
log.err()
return random.expovariate(1/1)
示例12: get_common_ancestor_waiting_time
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def get_common_ancestor_waiting_time(self, t):
"""
Returns the random waiting time until a common ancestor event
occurs within this population.
"""
ret = sys.float_info.max
k = self.get_num_ancestors()
if k > 1:
u = random.expovariate(k * (k - 1))
if self.growth_rate == 0:
ret = self.start_size * u
else:
dt = t - self.start_time
z = (
1
+ self.growth_rate
* self.start_size
* math.exp(-self.growth_rate * dt)
* u
)
if z > 0:
ret = math.log(z) / self.growth_rate
return ret
示例13: _fix
# 需要导入模块: import random [as 别名]
# 或者: from random import expovariate [as 别名]
def _fix(self, n=0):
o = random.choice(self.objlist)
if issubclass(o, ASN1_INTEGER):
return o(int(random.gauss(0,1000)))
elif issubclass(o, ASN1_IPADDRESS):
z = RandIP()._fix()
return o(z)
elif issubclass(o, ASN1_STRING):
z = int(random.expovariate(0.05)+1)
return o(bytes([random.choice(self.chars) for i in range(z)]))
elif issubclass(o, ASN1_SEQUENCE) and (n < 10):
z = int(random.expovariate(0.08)+1)
# return o(map(lambda x:x._fix(n+1), [self.__class__(objlist=self.objlist)]*z))
return o([ x._fix(n+1) for x in [self.__class__(objlist=self.objlist)]*z])
return ASN1_INTEGER(int(random.gauss(0,1000)))
##############
#### ASN1 ####
##############