0

我面临 python 的 random.expovariate() 的问题。

如果我运行以下代码: random.seed(44) for i in range(5): print random.expovariate(250.0/600.0)

我得到以下输出, 1.26037021316 1.87397995498 4.75384071801 0.466542950787 0.612235449166

但是,如果我在我的实际程序中使用相同的代码,如下所示,

import random
import simpy

RANDOM_SEED = 44
CUSTOMERS_TRANS = 250.0
BUSINESS_TRANS = 50.0
time_in_bank_customer = 2.0
time_in_bank_business = 4.0
arrive_time_bus = []
arrive_time_cus = []
wait_time_bus = []
wait_time_cus = []
serve_time_bus = []
serve_time_cus = []
expo_c=[]

def source_customer(env, counter, Rnd):
    """Source generates customers for customer transaction randomly"""
    for i in range(int(CUSTOMERS_TRANS)):
        env.process(customer(env, 'Customer%s' % (i + 1), counter, time_in_bank_customer, Rnd))
        r=Rnd.expovariate(CUSTOMERS_TRANS / 600.0)
        expo_c.append(r)
        yield env.timeout(r)

def source_business(env, counter, Rnd):
    """Source generates customers for business transaction randomly"""
    for j in range(int(BUSINESS_TRANS)):
        env.process(business(env, 'Business%s' % (j + 1), counter, time_in_bank_business, Rnd))
        yield env.timeout(Rnd.expovariate(BUSINESS_TRANS / 600.0))

def customer(env, name, counter, time_in_bank_customer, Rnd):
    """Customer arrives, is served and leaves."""
    arrive = env.now
    arrive_time_cus.append((name, arrive))
    print('%7.4f %s: Here I am' % (arrive, name))
    with counter.request() as req:
        yield req
        # patience = random.uniform(MIN_PATIENCE, MAX_PATIENCE)
        # Wait for the counter or abort at the end of our tether
        wait = env.now - arrive
        wait_time_cus.append((name,wait))
        # We got to the counter
        print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
        tib = Rnd.expovariate(1.0 / time_in_bank_customer)
        while tib < 0.5 or tib > 8:
            tib = Rnd.expovariate(1.0 / time_in_bank_customer)
        serve_time_cus.append((name,tib))
        yield env.timeout(tib)
        print('%7.4f %s: Finished' % (env.now, name))

def business(env, name, counter, time_in_bank_business, Rnd):
    """Customer arrives, is served and leaves."""
    arrive = env.now
    arrive_time_bus.append((name, arrive))
    print('%7.4f %s: Here I am' % (arrive, name))
    with counter.request() as req:
        yield req
        # Wait for the counter or abort at the end of our tether
        wait = env.now - arrive
        wait_time_bus.append((name,wait))
        # We got to the counter
        print('%7.4f %s: Waited %6.3f' % (env.now, name, wait))
        tib = Rnd.expovariate(1.0 / time_in_bank_business)
        while tib < 1 or tib > 15:
        tib = Rnd.expovariate(1.0 / time_in_bank_business)
        serve_time_bus.append((name,tib))
        yield env.timeout(tib)
        print('%7.4f %s: Finished' % (env.now, name))

# Scenario 1
def main():

    env = simpy.Environment()
    Rnd = random.Random(44)
    counter = simpy.Resource(env, capacity=1)
    env.process(source_customer(env, counter, Rnd))
    env.process(source_business(env, counter, Rnd))
    env.run()
    l = len(list(filter((lambda (x, y): y < 5), wait_time_cus))) + len(list(filter((lambda (x, y): y < 5), wait_time_bus)))
    print ('Number of processes that waited < 5 min = %s' % (l))
    # len(l) -> 8
    print arrive_time_bus
    print arrive_time_cus
    print wait_time_bus
    print wait_time_cus
    print serve_time_bus
    print serve_time_cus
    print expo_c

if __name__ == "__main__": main()

我得到了一个不同的 expo_c- 它本质上是相同行的输出。

我使用相同的种子,但 expovariate() 生成的序列不同。如何确保在我的实际程序中生成相同的序列?

4

0 回答 0