The title probably doesn't make a lot of sense, so let me lay out what I'm doing. I'm in the early stages of creating a framework for generating test data for a specific problem domain. One of the requirements is that a set of test data be easily re-generatable, i.e. if I create a set of test data, and then I want to provide the data set to someone else easily, or be able to regenerate that specific test data again, that's possible.
For various reasons, random selections are important in this process (to create reasonable variation from one run to the next). I would also like, for performance reasons, to be able to parallelize the process (i.e. I need to generate 1M records, spin up 8 threads that each do 125k records).
The simplest thing I can think of is to simply store (or at least display) the seed used for the RNG as part of each run, and allow the user to optionally provide a seed as input to a run. The project is being written in C# 4.5; it's a green field project and I have a lot of latitude to select libraries etc if necessary.
The conundrum I have is this:
Should I pass the seed around to the various classes that use Random to generate data, or pass the Random instance itself? Passing the seed seems a lot simpler with respect to thread safety etc, but seems problematic in other ways -- namely that if I kick off 8 threads with the same seed that are running the same operations in the same order, all 8 threads will produce identical output.
On the other hand, if I pass an instance of Random around, it seems I would have to use locks to synchronize access to it, and based on how frequent accesses to it are likely to be, there would be a lot of contention -- at which point it might not be any kind of performance win to parallelize at all (although if I'm streaming the output to disk at the same time, I/O might insert enough delays and pauses to rescue me). (Edit: I just realized that this would destroy the "repeatable" portion itself as there would be no way to guarantee that the threads called the Random instance in the same order. :facepalm
Is it worth trying to parallelize all this, and is there a good way to deal with the "repeatable random" nature desired of it, or should I just assume that parallelizing it is a hard problem and move ahead with just sharing a Random instance?
For various reasons, random selections are important in this process (to create reasonable variation from one run to the next). I would also like, for performance reasons, to be able to parallelize the process (i.e. I need to generate 1M records, spin up 8 threads that each do 125k records).
The simplest thing I can think of is to simply store (or at least display) the seed used for the RNG as part of each run, and allow the user to optionally provide a seed as input to a run. The project is being written in C# 4.5; it's a green field project and I have a lot of latitude to select libraries etc if necessary.
The conundrum I have is this:
Should I pass the seed around to the various classes that use Random to generate data, or pass the Random instance itself? Passing the seed seems a lot simpler with respect to thread safety etc, but seems problematic in other ways -- namely that if I kick off 8 threads with the same seed that are running the same operations in the same order, all 8 threads will produce identical output.
On the other hand, if I pass an instance of Random around, it seems I would have to use locks to synchronize access to it, and based on how frequent accesses to it are likely to be, there would be a lot of contention -- at which point it might not be any kind of performance win to parallelize at all (although if I'm streaming the output to disk at the same time, I/O might insert enough delays and pauses to rescue me). (Edit: I just realized that this would destroy the "repeatable" portion itself as there would be no way to guarantee that the threads called the Random instance in the same order. :facepalm
Is it worth trying to parallelize all this, and is there a good way to deal with the "repeatable random" nature desired of it, or should I just assume that parallelizing it is a hard problem and move ahead with just sharing a Random instance?