def __init__(self, seed=None):
Data.__init__(self)
self.output = ''
self.yesno_callback = False
self.yesno_casual = False # whether to insist they answer
self.clock1 = 30 # counts down from finding last treasure
self.clock2 = 50 # counts down until cave closes
self.is_closing = False # is the cave closing?
self.panic = False # they tried to leave during closing?
self.is_closed = False # is the cave closed?
self.is_done = False # caller can check for "game over"
self.could_fall_in_pit = False # could the player fall into a pit?
self.random_generator = random.Random()
if seed is not None:
self.random_generator.seed(seed)
python类random()的实例源码
def rng(self):
cur_pid = _os.getpid()
if cur_pid != getattr(self, '_rng_pid', None):
self._rng = _Random()
self._rng_pid = cur_pid
return self._rng
def give_port():
"""
Returns a random port and registers it.
"""
global port_random
context = get_deploy_context()
# default behavior
if context["config"] is None:
return randint(1000, 65000)
if "banned_ports_parsed" not in context["config"]:
banned_ports_result = []
for port_range in context["config"].banned_ports:
banned_ports_result.extend(list(range(port_range["start"], port_range["end"] + 1)))
context["config"]["banned_ports_parsed"] = banned_ports_result
# during real deployment, let's register a port
if port_random is None:
port_random = Random(context["config"].deploy_secret)
# if this instance already has a port, reuse it
if (context["problem"], context["instance"]) in inv_port_map:
return inv_port_map[(context["problem"], context["instance"])]
if len(context["port_map"].items()) + len(context["config"].banned_ports_parsed) == 65536:
raise Exception("All usable ports are taken. Cannot deploy any more instances.")
while True:
port = port_random.randint(0, 65535)
if port not in context["config"].banned_ports_parsed:
owner, instance = context["port_map"].get(port, (None, None))
if owner is None or (owner == context["problem"] and instance == context["instance"]):
context["port_map"][port] = (context["problem"], context["instance"])
return port
def update_problem_class(Class, problem_object, seed, user, instance_directory):
"""
Changes the metaclass of the given class to introduce necessary fields before
object instantiation.
Args:
Class: The problem class to be updated
problem_name: The problem name
seed: The seed for the Random object
user: The linux username for this challenge instance
instance_directory: The deployment directory for this instance
Returns:
The updated class described above
"""
random = Random(seed)
attributes = deepcopy(problem_object)
# pass configuration options in as class fields
attributes.update(dict(deploy_config))
attributes.update({"random": random, "user": user, "directory": instance_directory,
"server": deploy_config.hostname})
return challenge_meta(attributes)(Class.__name__, Class.__bases__, Class.__dict__)
def initial_state(self):
r = Random()
return [r.randint(1, 8) for y in range(8)]
def crossover(self, p1, p2):
r = Random()
crossover_index = r.randint(0, 8)
left = p1.state[0:crossover_index]
right = p2.state[crossover_index:8]
left.extend(right)
return left
def mutate(self):
r = Random()
for i in range(len(self.state)):
if random() < self._mutation_rate:
self.state[i] = r.randint(1,8)
def mutate(self):
r = Random()
if random() < self._mutation_rate:
ix1 = r.randint(0,len(self.state)-1)
ix2 = r.randint(0,len(self.state)-1)
temp = self.state[ix1]
self.state[ix1] = self.state[ix2]
self.state[ix2] = temp
def __init__(self):
self.mutex = _allocate_lock()
self.rng = _Random()
self.normcase = _os.path.normcase
def __init__(gen, rng=1, chain=None, **kwargs):
if not hasattr(rng, 'randrange'):
import random
rng = random.Random(rng)
if chain is None:
chain = Abe.Chain.create("Testnet")
gen._rng = rng
gen.chain = chain
for attr, val in kwargs.items():
setattr(gen, attr, val)