当前位置: 首页>>代码示例>>Python>>正文


Python Random.expovariate方法代码示例

本文整理汇总了Python中random.Random.expovariate方法的典型用法代码示例。如果您正苦于以下问题:Python Random.expovariate方法的具体用法?Python Random.expovariate怎么用?Python Random.expovariate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在random.Random的用法示例。


在下文中一共展示了Random.expovariate方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: compress_string

# 需要导入模块: from random import Random [as 别名]
# 或者: from random.Random import expovariate [as 别名]
def compress_string(s):

    # avg_block_size is acutally the reciporical of the average
    # intended interflush distance.   

    rnd = Random(s)

    flushes_remaining = FLUSH_LIMIT

    if len(s) < AVERAGE_SPAN_BETWEEN_FLUSHES * APPROX_MIN_FLUSHES:
        avg_block_size = APPROX_MIN_FLUSHES / float(len(s) + 1)
    else:
        avg_block_size = 1.0 / AVERAGE_SPAN_BETWEEN_FLUSHES

    s = StringIO(s) if isinstance(s, six.text_type) else BytesIO(s)
    zbuf = BytesIO()
    zfile = GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)
    chunk = s.read(MIN_INTERFLUSH_INTERVAL + int(rnd.expovariate(avg_block_size)))
    while chunk and flushes_remaining:
        zfile.write(chunk)
        zfile.flush()
        flushes_remaining -= 1
        chunk = s.read(MIN_INTERFLUSH_INTERVAL + int(rnd.expovariate(avg_block_size)))
    zfile.write(chunk)
    zfile.write(s.read())
    zfile.close()
    return zbuf.getvalue()
开发者ID:miki725,项目名称:breach_buster,代码行数:29,代码来源:gzip.py

示例2: generate

# 需要导入模块: from random import Random [as 别名]
# 或者: from random.Random import expovariate [as 别名]
 def generate(self, number, interval):       
     rv = Random(self.SEED)
     for i in range(number):
         c = Customer(name = 'Customer%02d' % (i,))
         activate(c, c.visit(timeInBank = 12.0))
         t = rv.expovariate(1.0 / interval)
         yield hold, self, t
开发者ID:prajeshbhat,项目名称:simulacao-trabalho,代码行数:9,代码来源:SimGUI.py

示例3: compress_sequence

# 需要导入模块: from random import Random [as 别名]
# 或者: from random.Random import expovariate [as 别名]
def compress_sequence(sequence):
    avg_block_size = 1.0 / AVERAGE_SPAN_BETWEEN_FLUSHES

    buf = StreamingBuffer()
    zfile = GzipFile(mode='wb', compresslevel=6, fileobj=buf)
    # Output headers...
    yield buf.read()

    flushes_remaining = FLUSH_LIMIT
    rnd = None
    count = None
    rnd = None
    for item in sequence:
        if rnd is None:
            rnd = Random(hash(item))
            count = int(rnd.expovariate(avg_block_size))
        chunking_buf = BytesIO(item)
        chunk = chunking_buf.read(count)
        while chunk:
            if count is not None:
                count -= len(chunk)
            zfile.write(chunk)
            if count <= 0:
                flushes_remaining -= 1
                zfile.flush()
                yield buf.read()
                if flushes_remaining:
                    count = int(rnd.expovariate(avg_block_size))
                else:
                    count = None
            if count is None:
                chunk = chunking_buf.read()
            else:
                chunk = chunking_buf.read(count)
        zfile.flush()
        yield buf.read()
        if chunk is None:
            break
        
    for item in sequence:
        zfile.write(chunking_buf.read())
        zfile.flush()
        yield buf.read()
        
    zfile.close()
    yield buf.read()
开发者ID:miki725,项目名称:breach_buster,代码行数:48,代码来源:gzip.py

示例4: Generator

# 需要导入模块: from random import Random [as 别名]
# 或者: from random.Random import expovariate [as 别名]
class Generator(Process):
 """ generates a sequence of msgs """    
 def __init__(self, rate,maxT,maxN):
     Process.__init__(self)
     self.name = "Generator"
     self.rate = rate
     self.maxN = maxN
     self.maxT = maxT
     self.g = Random(11335577)
     self.i = 0
     
 def execute(self):
     while (now() < self.maxT)  & (self.i < self.maxN):
         self.i+=1
         p = Msg(self.i,startNode)
         activate(p,p.execute())
         ## self.trace("starting "+p.name)
         yield hold,self,self.g.expovariate(self.rate)
     self.trace("generator finished with "+`self.i`+" ========")
     self.stopSim()
     
 def trace(self,message):
     if GTRACING: print "%7.4f \t%s"%(now(), message)
开发者ID:peterlharding,项目名称:PDQ,代码行数:25,代码来源:jacksim.py

示例5: Generator

# 需要导入模块: from random import Random [as 别名]
# 或者: from random.Random import expovariate [as 别名]
class Generator(Process):
    """ generates a sequence of calls """

    def __init__(self, maxN, lam):
        global busyEndTime
        Process.__init__(self)
        self.name = "generator"
        self.maxN = maxN
        self.lam = lam
        self.iatime = 1.0 / lam
        self.rv = Random(gSeed)
        busyEndTime = now()  # simulation start time

    def execute(self):
        for i in range(self.maxN):
            j = Job(i)
            activate(j, j.execute())
            yield hold, self, self.rv.expovariate(lam)
        self.trace("WARNING generator finished -- ran out of jobs")

    def trace(self, message):
        if GTRACING:
            print "%7.4f \t%s" % (self.time(), message)
开发者ID:lglmoura,项目名称:PyDDSIM,代码行数:25,代码来源:celphone.py

示例6: simulate

# 需要导入模块: from random import Random [as 别名]
# 或者: from random.Random import expovariate [as 别名]
def simulate(n, rho, theta, delta, debug=False):
    """Perform simulation.

    Be aware that the nodes in activeLineages are
    quite distinct from the nodes that eventually wind up on the graph.
    The former are _represented_ by node objects, but these objects have
    no definite height.  Coalescence leaves anihiliates two lineages leaving
    one lineage and one node remaining, while conversion results in a duplication
    of lineages but leaves only a single node (with two parents) on the graph."""

    rand = Random()

    activeLineages = []
    for i in range(n):
        node = Node()
        node.setLabel(i)
        lineage = Node() # Lineages are separate to nodes on the graph!
        lineage.addChild(node)
        activeLineages.append(lineage)

    t = 0.0

    # Simulation loop
    while len(activeLineages)>1:
        n = len(activeLineages)

        # Coalescence propensity
        cProp = theta*0.5*n*(n-1)
        
        # Recombination/conversion propensity
        rProp = rho*n

        # Choose time of next event
        totProp = cProp + rProp
        t += rand.expovariate(totProp)

        # Select type of event
        if rand.uniform(0,totProp)<cProp:
            # Coalescence

            # Select random pair of lineages:
            lineages = rand.sample(activeLineages, 2)
            
            # Coalesce nodes:
            parent = Node()
            parent.height = t

            parent.addChild(lineages[0])
            parent.addChild(lineages[1])
            parent.ancestral = ancestralUnion(lineages[0].ancestral, lineages[1].ancestral)

            # Replace coalesced nodes by parent node in active lineages:

            activeLineages.remove(lineages[0])
            activeLineages.remove(lineages[1])
            lineages[0].deleteLineage()
            lineages[1].deleteLineage()

            parentLineage = Node()
            parentLineage.addChild(parent)
            parentLineage.ancestral = parent.ancestral
            activeLineages.append(parentLineage)

        else:
            # Recombination/conversion

            # Select lineage at random
            lineage = rand.sample(activeLineages, 1)[0]

            # Select start and end of converted region:
            boundary1 = rand.uniform(0,1)
            if (rand.uniform(0,1)<0.5):
                boundary2 = min(1,boundary1 + rand.expovariate(1/delta))
            else:
                boundary2 = max(0,boundary1 - rand.expovariate(1/delta))
            boundary1, boundary2 = sorted([boundary1,boundary2])

            # Partition ancestral material:
            newAncestrals = ancestralPartition(lineage.ancestral, (boundary1,boundary2))

            # Continue only if conversion has effect:
            if len(newAncestrals[0])>0 and len(newAncestrals[1])>0:

                if debug:
                    print "t={}: Conversion: {} => {} {}".format(t, lineage.ancestral, newAncestrals[0], newAncestrals[1])

                # Set original node height:
                lineage.height = t
                
                # Generate parents:
                parent1 = Node()
                parent1.addChild(lineage)
                parent1.ancestral = newAncestrals[0]

                parent2 = Node()
                parent2.addChild(lineage)
                parent2.ancestral = newAncestrals[1]

                # Now that the lineage node is on the graph, ensure its child has
                # a corresponding ancestralParents entry:
#.........这里部分代码省略.........
开发者ID:tgvaughan,项目名称:ConvSim,代码行数:103,代码来源:ConvSim.py


注:本文中的random.Random.expovariate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。