[Tutor] Generating random numbers
Tim Peters
tim.one at comcast.net
Thu Dec 4 14:23:14 EST 2003
[Venkatesh Babu]
> I have a question related to generation of random
> numbers based on some probability distribution
>
> In my simulation program I have to generate random
> numbers based on same kind of distribution, say normal
> distribution, but for different independent purposes:
> like determining number of agents in a place,
> determination ages of each agent. So, there are more
> than one normal distributions, each having its own
> mean and variance. In such a situation I guess we
> should create "Random" objects corresponding to each
> purpose and call normalvariate function of these
> objects with the corresponding mean and variance.
>
> Is the above method proper or can we do away by just
> calling the normalvariate function given in the random
> module instead of creating "Random" objects.
Calling random.normalvariate(mu, sigma) for all purposes is fine (although
calling random.gauss(mu, sigma) is faster -- gauss() was added after
normalvariate(), and normalvariate() sticks around so that older programs
using it can continue to get bit-for-bit identical results).
The only really good reason to create your own Random objects is if you're
running multiple threads, in which case having each thread use its own
Random object makes it very much easier to get exactly reproducible (across
distinct program runs) results.
More information about the Tutor
mailing list