0

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?

asked Sep 5, 2014 at 23:36
2
  • 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. Commented 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. Commented Sep 8, 2014 at 0:09

1 Answer 1

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.

answered Sep 8, 2014 at 13:54
Sign up to request clarification or add additional context in comments.

4 Comments

I simulated Ans(1) & got 10 different sets of random number which are neither identical to each other nor repeatable. These will become pseudorandom numbers if the random numbers get repeated for the same seed. But, this is not what happens in (1) or should I be saving the seed? This is exactly the problem that I had been facing which is even by using rng() instead of getting repeatable numbers, I am getting all different sets. Did I not understand correctly>
@SKM: I am confused by your question. It seems that this isn't working for you, but you also accepted my answer –which is it? Yes, you should be saving the seed. That's the whole idea of a seed: it simplifies resetting a random number generator down to a single number. Each time your run the entire code in #1 above (starting at 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.
I accepted your answer because it goes according to the theory, but I am unable to generate the same random number in my machine. Also, in order to save the seed should I not be using a variable like mySeed = rng(1)? I really don't know why the random numbers are not repeatable in my end.
@SKM: This is answered in the documentation for 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.