python类random()的实例源码

adventure.py 文件源码 项目:Dumb-Cogs 作者: irdumbs 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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)
tempfile.py 文件源码 项目:python- 作者: secondtonone1 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
deploy.py 文件源码 项目:picoCTF 作者: picoCTF 项目源码 文件源码 阅读 67 收藏 0 点赞 0 评论 0
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
deploy.py 文件源码 项目:picoCTF 作者: picoCTF 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
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__)
state.py 文件源码 项目:8-Queens 作者: miguelarauj1o 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def initial_state(self):
        r = Random()
        return [r.randint(1, 8) for y in range(8)]
state.py 文件源码 项目:8-Queens 作者: miguelarauj1o 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
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
state.py 文件源码 项目:8-Queens 作者: miguelarauj1o 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def mutate(self):
        r = Random()
        for i in range(len(self.state)):
            if random() < self._mutation_rate:
                self.state[i] = r.randint(1,8)
state.py 文件源码 项目:8-Queens 作者: miguelarauj1o 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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
tempfile.py 文件源码 项目:kinect-2-libras 作者: inessadl 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __init__(self):
        self.mutex = _allocate_lock()
        self.rng = _Random()
        self.normcase = _os.path.normcase
datagen.py 文件源码 项目:abe-bootstrap 作者: TryCoin-Team 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
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)


问题


面经


文章

微信
公众号

扫码关注公众号