Dispatching functions from a dictionary

tkpmep at gmail.com tkpmep at gmail.com
Sun Mar 30 16:54:50 EDT 2008


To keep a simulation tidy, I created a dispatcher that generates
random variables drawn from various distributions as follows:

import random

RVType = 1          #Type of random variable - pulled from RVDict

RVDict= {'1': random.betavariate(1,1),     '2': random.expovariate(1),
         '3': random.gammavariate(1,1),      '4': random.gauss(0,1),
         '5': random.lognormvariate(1,1),     '6':
random.paretovariate(1),
         '7': random.uniform( -1,1),             '8':
random.weibullvariate(1,2) }

x = []
y=[]

rv = RVDict[str(RVType)]
for i in range(N):
    x.append(rv)
    y.append(rv)


Oddly, x and y get filled with a single value repeated N times. I
expected to get a different random number appear each time I called
rv ,but this does not happen. Instead, my first call to rv generates a
random number from the appropriate distribution, while subsequent
calls simply repeat the random number generated in the first call.
Where am I going wrong?

Thanks in advance for your help.

Sincerely


Thomas Philips




More information about the Python-list mailing list