# Two questions about my code: 1.) Estimating random number with and without rng ('shuffle'); 2.) storaging results

1 view (last 30 days)
Mustafa Vural on 3 Sep 2020
Commented: Mario Malic on 3 Sep 2020
I want to estimate random numbers with the weibull distribution.
1.) "wblrnd" generates random numbers of my function "custompdf". So do "rng ('shuffle')".
In "results" I can see my results. When I run my code(without changing it), it displays random numbers.
When I I delete the line with rng ('shuffle'), it also display random numbers.
So whats the difference?
2.) Someone helped me to storage my code for a better overview and for flexibilty of "b" and "T" (In case I change them)
Its the line beginning with "results". It works really nice, but I dont really understand the line. Can someone explain what the line is doing? I am really a noob in Matlab.
clear all;
n = 100;
t0 = 0.5;
b = 1:3;
T = 1:3;
rng ('shuffle')
for v_T= T
for v_b= b
data(:,v_b,v_T) = wblrnd(v_b,v_T, [n,1]) + t0;
start = [1 0 0];
custompdf = @(x,a,b,c) (x>c).*(b/(a-c)).*(((x-c)/(a-c)).^(b-1)).*exp(-((x-c)/(a-c)).^b);
opt = statset('MaxIter',1e3,'MaxFunEvals',1e3,'FunValCheck','off');
params(v_b,1:3,v_T) = mle(data(:,v_b,v_T),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',[0 0 0],'UpperBound',[Inf Inf min(data(:,v_b,v_T))])
params(v_b,4,v_T) = v_b;
params(v_b,5,v_T) = v_T;
params(v_b,6,v_T) = t0;
params(v_b,7,v_T) = n;
end
results((v_T-1)*length(b)+1:v_T*length(b), 1:size(params, 2)) = params(:,:,v_T);
end

Mustafa Vural on 3 Sep 2020
Hi,
thank you for the link, but that doesnt really help me.
Dana on 3 Sep 2020
Here's what I did:
1. Close all open instances of Matlab.
2. Open Matlab.
3. Type rand, which returns 0.81472.
4. Close Matlab.
5. Open Matlab again.
6. Type rand, which again returns 0.81472.
So at least for my version of Matlab, without using rng('shuffle') I get the same random number draw each time I re-open Matlab. Try the exact above experiment and tell me whether you get a different number on the second rand call. If so, just enter rng and post the output, and maybe we can diagnose the problem. For me, that looks like:
>> rng
ans =
struct with fields:
Type: 'twister'
Seed: 0
State: [625×1 uint32]
Mustafa Vural on 3 Sep 2020
First I open Matlab and then type rand, result was: 0.094... and then everytime I close and open= 0.8147
But if you run my code, and copy out the result, run again, copy again. Like 2 or 3 times. Then close matlab, then open matlab again and run the code again, it generates whole other random numbers. Just try

Steven Lord on 3 Sep 2020
For the first question, when you start MATLAB the "deck" of random numbers (technically it's a stream, but deck fits the metaphor better) is in a known order. When you generate a random number ("pull the top card of the deck") that first number is always the same. [When you use rng default that "stacks the deck" back to this ordering.]
You can think of rng shuffle as shuffling that "deck" to a new order. [Technically it's probably more similar to cutting the deck at a point based on the current time and date but it's a metaphor. It doesn't have to be a 100% accurate representation of reality.]
If the deck had been stacked before, it wouldn't be now. So you'd go from knowing exactly which number you'd generate next to not knowing.
If the deck hadn't be stacked before, it still won't be. So you'd go from not knowing which number is next to still not knowing which is next.

Mario Malic on 3 Sep 2020
Edited: Mario Malic on 3 Sep 2020
I agree with Dana and David. Here's my contribution to the rand as well.
>> rand
ans =
0.8147
About the second question: params is a 3D array, and it assigns values from v_T "page" to the results array.

Mustafa Vural on 3 Sep 2020
Yes I know what the line is for, but I dont unterstand the structure. What does ...1:size(params, 2)) mean? Especially the 2?
And why "(v_T-1)"?
Mario Malic on 3 Sep 2020
For first question, checking the documentation you will get an answer faster.
1:size(params, 2)) - numbers from 1 to size of params in column direction.:
%
(v_T-1)*length(b)+1:v_T*length(b), 1:size(params, 2)
For v_T == 1 it will assign values to indices 1:3
For v_T == 2 it will assign values to indices 4:6 and so on.
There is probably a better way to rewrite this to look more understandable.