How do I generate time series from PRNG for T independent runs of the experiment? Will the seed be different for each T? rand() or randn() will generate random numbers but if the initial condition is the same or if the seed is fixed then it becomes deterministic or pseudo random.
Can somebody please show how to generate PRNG? Will randn(seed) where seed = randn() generate PRNG?
- 
 This and this might be helpful. Also, I suggest editing your question to focus on one specific topic. I think that you can ask about random number generation without going into all of the details of your filter design.horchler– horchler2014年09月06日 00:40:41 +00:00Commented Sep 6, 2014 at 0:40
 - 
 @horchler: Thank you for the suggestion to edit the Question. Also, I studied the links that you provided and I had mentioned in my question regarding the doubts on using rand() or randn() for random number generation. The problem is in the area of pseudo random number generation. Instead of using s = rng I tried using a fixed number but that does not repeat the random number sequences.SKM– SKM2014年09月08日 00:09:07 +00:00Commented Sep 8, 2014 at 0:09
 
1 Answer 1
There are are several ways that are commonly used to generate T independent runs of pseudorandom variates. The first thing to note is that, for a given seed value, each variate produced by rand (or randn, etc.) is independent from each other, i.e., they are independent and identically distributed (i.i.d.). This means that in many cases you can set your random number generator's seed via rng once and then the code will produce repeatable pseudorandom values each time it is run:
1.
Generate T independent runs of N uniformly distributed values – each time you run this, the same random values will be generated in the same order as long as you call rng with the same argument at the beginning:
rng(1); % Set seed to 1, calls to rand, randn are now based on this seed
N = 100;
T = 10;
for i = 1:T
 r = rand(N,1);
 % Do something with r
end
2.
Allocating all of the random variates at once is faster. The columns of r below will be identical to each r in the for loop above:
rng(1);
N = 100;
T = 10;
r = rand(N,T);
for i = 1:T
 % Do something with r(:,i)
end
3.
Sometimes you want to be able to reproduce an arbitrary run without needing to simulate all T of them. One way you can accomplish this is by incrementing the seed value of rng for each run. The values in r will be different from those in the two cases above. Also, be aware that resetting the seed is an expensive operation so doing this will slow down your code a bit (in fact, you should avoid resetting the seed as much as possible):
N = 100;
T = 10;
for i = 1:T
 rng(i);
 r = rand(N,1);
 % Do something with r
end
4. Sometimes we may wish to simulate T runs across multiple CPUs and generate random variates in parallel on each of those CPUs. See my answer here for details on you might do this. It may be simpler and more robust to generate all of the variates on one CPU and then share the appropriate parts of the resulting array with each of the parallel process. Distributed arrays can facilitate this.
The documentation for random number generation in Matlab is extensive and there are many advanced features. I suggest reading more and trying some of the examples. This video might also be helpful.
4 Comments
rng(1);) the series of random values will be exactly the same. In many cases (i.e, #1 and #2 in my answer) you won't need more than one seed (which you can hard code in your file) unless you want to see the result of changing it.rng. The input is the new seed value. The function returns the previous seed as a struct. Just create a vector of positive integers for your seed values and use those – no need to save any output from rng. That's all example #3 does with the for loop index.