Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing common random numbers in a simulation

I am building a small simulation in Python and I would like to use Common Random Numbers to reduce variation. I know that I must achieve synchronization for CRN to work:

CRN requires synchronization of the random number streams, which ensures that in addition to using the same random numbers to simulate all configurations, a specific random number used for a specific purpose in one configuration is used for exactly the same purpose in all other configurations.

I was wondering if the way I wanted to implement it in my simulation was valid or if I should be using a different approach.

My simulation has three different classes (ClassA, ClassB, ClassC), and ClassA objects have random travel times, ClassB objects have random service times and random usage rates, and ClassC objects have random service times. Of course there can be multiple instances of each class of object.

At the start of the simulation I specify a single random number seed (replication_seed) so that I can use a different seed for each simulation replication.

import numpy.random as npr
rep_rnd_strm = npr.RandomState().seed(replication_seed)

Then in the constructor for each Class, I use rep_rnd_strm to generate a seed that is used to initialize the random number stream for the instance of the class:

self.class_rnd_strm = npr.RandomState().seed(rep_rnd_strm.randint(10000000))

I then use self.class_rnd_strm to generate a seed for each random number stream needed for the class instance. For example the constructor of ClassA has:

self.travel_time_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))

while the constructor of ClassB has:

self.service_time_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))
self.usage_rate_strm = npr.RandomState().seed(self.class_rnd_strm.randint(10000000))

Is what I am doing here a valid approach to getting synchronization to work, or should I be doing things differently?

like image 606
derNincompoop Avatar asked Mar 12 '15 14:03

derNincompoop


People also ask

Why random numbers are used in simulation?

Random numbers enable a simulation to include the variability that occurs in real life. Each place where random numbers are used within a simulation uses a separate stream of random numbers.

How is random number generator implemented?

Example Algorithm for Pseudo-Random Number GeneratorAccept some initial input number, that is a seed or key. Apply that seed in a sequence of mathematical operations to generate the result. That result is the random number. Use that resulting random number as the seed for the next iteration.


1 Answers

Yes. That is a valid approach to make it replicable, but only if you can guarantee that there is no randomness in the order in which the various instances of the various classes are instantiated. This is because if they are instantiated in a different order, then they will get a different seed for their random number generator.

like image 151
patapouf_ai Avatar answered Oct 14 '22 19:10

patapouf_ai