reproduce random numbers for the third dimension

Hello
I want to generate random numbers for matrix A which has a second page(i.e. 2), whose values i am going to assign to another varaiable in my code. Though i have the rng command to reproduce the same random numbers, the very numbers in the second page do vary in every iteration..How can I regenerate the same random numbers in each iteration for the second page of my matrix A? Any idea is highly appreciated.
rng('default');
rng(1);
Nmc=200
uu=1
A=rand(Nmc,uu+1,2);

4 Comments

I don't understand. Can you clarify with maybe a small example? E.g., suppose you had a 2x2x2 variable A. How do you want this to vary from iteration to iteration?
Thanks for your reply. In fact, I am doing a Monte Carlo simulation. In each iteration, i generate random values for two random variables, e.g. c and d. In this regard, i initially produce uniform random numbers for both c and d(which are then converted to lognormal random variables). so, matrix A should include 200 random numbers for each random variable (each iteration with a different random number for c and d). The second number (i.e. uu+1) is the number of random variables (here, it is 2). so, matrix A=rand(200,2,2). The last 2 in the preceding paranthesis is associated with the random numbers generated for the second random variable(i.e. d). I mean, I do not want to use the same uniform random numbers for both of my random variables.
My question is, How can I reproduce the same random numbers for my second random variable?
If I understand what you want to do correctly, it is quite simple.
% Set the seed
rng default
% Set the parameters. (I chose small Nmc for illustration)
Nmc=3;
uu=1;
% Preallocate memory for the whole array.
% (Probably only important if you are going to create many slices, or large arrays.)
A = zeros(Nmc,uu+1,2);
% Generate the first slice at random
A(:,:,1) = rand(Nmc,uu+1,1);
% Repeat the first set as the second
A(:,:,2) = A(:,:,1);
% Show the result
disp(A)
(:,:,1) = 0.8147 0.9134 0.9058 0.6324 0.1270 0.0975 (:,:,2) = 0.8147 0.9134 0.9058 0.6324 0.1270 0.0975
Thanks for your reply. However, let me explain a bit more clearly. in the first itertaion (i.e. Nmc=1) , i need
(:,:,1) =
.109 .2
.323 .50
.15 .6
and
(:,:,2) =
.17 .16
.84 .91
.153 .65
Again in the second iteration(i.e. Nmc=2), i want:
(:,:,1) =
.109 .2
.323 .50
.15 .6
and
(:,:,2) =
.17 .16
.84 .91
.153 .65

Sign in to comment.

 Accepted Answer

If you don't want the random numbers to change from iteration to iteration, then don't regenerate them inside the loop. Calculate them once, before the loop starts, and then reuse them over and over within the loop.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!