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
python类expovariate()的实例源码
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
def do_churn(self):
"""Simulate churn: delete and add nodes from/to the network."""
nAdd = int(random.expovariate(self._lamdbaAdd) + 0.5)
nDel = int(random.expovariate(self._lamdbaDel) + 0.5)
# kill nDel non-dead nodes at random.
random.shuffle(self._wholenet)
nkilled = 0
for node in self._wholenet:
if nkilled == nDel:
break
if not node._dead:
node.kill()
nkilled += 1
# add nAdd new nodes.
for n in xrange(self._total, self._total+nAdd):
node = Node("node%d"%n,
port=_randport(),
evil=random.random() < self._pevil)
self._total += 1
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
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
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 ####
##############
def sim_configuration(env, num_machines,
washing_time,
arrival_time):
''' In this routine: washing machines are created, an initial number of
vehicles (2) and then vehicles arrive at a rate of 1 / arrival_time'''
# Create a washing machine
machine = WashingMachine(env, num_machines, washing_time)
# 2 vehicles are created and put in to the queue
print('In the queue there are already 2 vehicles')
for i in range(2):
env.process(vehicle(env, 'Vehicle {0}'.format(i), machine))
# While the machine runs vehicles arrive at a rate of 1 / arrival_time
while True:
yield env.timeout(round(random.expovariate(1 / arrival_time)))
i += 1
env.process(vehicle(env, 'Vehicle {0}'.format(i), machine))
# Simulation configuration
# random.seed(RANDOM_SEED)
# Simulation environment creation
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 ####
##############
def check_user(user: User, password: str) -> bool:
hashpass, salt = user_info[user].hashed_password
target_hash_pass = hash_password(password, salt)[0]
sleep(random.expovariate(10))
return secrets.compare_digest(hashpass, target_hash_pass)
def _fix(self):
return self.base+int(round(random.expovariate(self.lambd)))
def _fix(self):
return self.base+int(round(random.expovariate(self.lambd)))
def test_sleep(self):
for i in xrange(10):
length = random.expovariate(1/0.1)
start = time.time()
yield deferral.sleep(length)
end = time.time()
assert length <= end - start <= length + 0.1
def test_all(self):
for test in ['', 'a', 'b', 'abc', 'abc'*50, 'hello world']:
#print test
#print sha256.sha256(test).hexdigest()
#print hashlib.sha256(test).hexdigest()
#print
assert sha256.sha256(test).hexdigest() == hashlib.sha256(test).hexdigest()
def random_str(l):
return ''.join(chr(random.randrange(256)) for i in xrange(l))
for length in xrange(150):
test = random_str(length)
a = sha256.sha256(test).hexdigest()
b = hashlib.sha256(test).hexdigest()
assert a == b
for i in xrange(100):
test = random_str(int(random.expovariate(1/100)))
test2 = random_str(int(random.expovariate(1/100)))
a = sha256.sha256(test)
a = a.copy()
a.update(test2)
a = a.hexdigest()
b = hashlib.sha256(test)
b = b.copy()
b.update(test2)
b = b.hexdigest()
assert a == b
def get_good_peers(self, max_count):
t = time.time()
return [x[0] for x in sorted(self.addr_store.iteritems(), key=lambda (k, (services, first_seen, last_seen)):
-math.log(max(3600, last_seen - first_seen))/math.log(max(3600, t - last_seen))*random.expovariate(1)
)][:max_count]
def _fix(self):
return self.base+int(round(random.expovariate(self.lambd)))
def compute_duration(previous_action):
"""Compute action duration using exponential distribution"""
if previous_action in ['leave garage', 'drop off passenger']:
# new state is prowling
interval = SEARCH_DURATION
elif previous_action == 'pick up passenger':
# new state is trip
interval = TRIP_DURATION
elif previous_action == 'going home':
interval = 1
else:
raise ValueError('Unknown previous_action: %s' % previous_action)
return int(random.expovariate(1/interval)) + 1
def compute_delay(interval):
"""Compute action delay using exponential distribution"""
return int(random.expovariate(1/interval)) + 1
# BEGIN TAXI_PROCESS
def test_sleep(self):
for i in xrange(10):
length = random.expovariate(1/0.1)
start = time.time()
yield deferral.sleep(length)
end = time.time()
assert length <= end - start <= length + 0.1
def test_all(self):
for test in ['', 'a', 'b', 'abc', 'abc'*50, 'hello world']:
#print test
#print sha256.sha256(test).hexdigest()
#print hashlib.sha256(test).hexdigest()
#print
assert sha256.sha256(test).hexdigest() == hashlib.sha256(test).hexdigest()
def random_str(l):
return ''.join(chr(random.randrange(256)) for i in xrange(l))
for length in xrange(150):
test = random_str(length)
a = sha256.sha256(test).hexdigest()
b = hashlib.sha256(test).hexdigest()
assert a == b
for i in xrange(100):
test = random_str(int(random.expovariate(1/100)))
test2 = random_str(int(random.expovariate(1/100)))
a = sha256.sha256(test)
a = a.copy()
a.update(test2)
a = a.hexdigest()
b = hashlib.sha256(test)
b = b.copy()
b.update(test2)
b = b.hexdigest()
assert a == b
def _think(self):
try:
if len(self.addr_store) < self.preferred_storage and self.peers:
random.choice(self.peers.values()).send_getaddrs(count=8)
except:
log.err()
return random.expovariate(1/20)