## Random numbers from randsample suddenly repeating

Hey,

I have a problem with my random numbers that suddenly start to repeat.

Unfortunately I cant chare my code that generates the problem. I scaled it down to something that actually does the same, but somehow the problem does not appear…

But maybe someone can still figure out whats my problem is…

So here is my example code that visuallizes whats going on.

x=-10:0.2:10;

[x1,x2]=meshgrid([-10:0.2:10],[-10:0.2:10]);

close all

figure

xlim([-10 10])

ylim([-10 10])

for i=1:400

y=pdf(‘Normal’,x,0,0.3+0.05*sqrt(i));

Nuc_id=[randsample( [1:numel(P)] , 16, true, P(:))]’;

[row,col]=ind2sub(size(P),Nuc_id);

P=f*y.*y’;

hold off

imagesc(P)

hold on

scatter(row,col,10,’r’,’filled’)

pbaspect([1 1 1])

pause(0.1)

end

I have a probability function P that changes over time depending on multiple conditions. In the example code I just changed sigma with the rising index, that should do just fine compared that the original way of calculating P.

Then I take random values from P, based on the probability of P itsself. In this case its 16 random indices.

here, as in my originla Code it works just fine, but in my originla code, after multiple iterations, these 16 rnd indices suddenly stop changing, from one iteration to the next. its like 16 rnd numbers for 200 iterations and suddenly its the same 16 rdn numbers 5 times in a row, sometimes only 15/16 are the same and the one that is different differs just by one, like index 567 instead of 568 or so. Then after 5 times repeating those rnd numbers, all 16 rnd numbers are changed to 16 new rnd numbers, but those also repeat a few times. And after doing this 2 to 4 times, the behaviour stops as sudden as it startet and the rnd numbers are perfectly random again…

I can run the code and visually see when it happens, when I breakpoint the code after "randsample" I can show that the rand number did not change compared to the previous iteration, but then when i manually copy the randsample line inthe command window while the code is paused, the rand numbers are rnd again…

So i realy cant trace back where the problem is comming from, but maybe somebody has an idea.

PS: Not sure if related th this post:

https://de.mathworks.com/matlabcentral/answers/1680149-why-do-the-random-numbers-repeat-in-each-run?s_tid=prof_contriblnk

But I do not have a problem when some numbers or the whole 16 numbers would repeat somewhere or sometimes, but not 5 times in a row out of nothing…

Many thanks in advance

Best regardsHey,

I have a problem with my random numbers that suddenly start to repeat.

Unfortunately I cant chare my code that generates the problem. I scaled it down to something that actually does the same, but somehow the problem does not appear…

But maybe someone can still figure out whats my problem is…

So here is my example code that visuallizes whats going on.

x=-10:0.2:10;

[x1,x2]=meshgrid([-10:0.2:10],[-10:0.2:10]);

close all

figure

xlim([-10 10])

ylim([-10 10])

for i=1:400

y=pdf(‘Normal’,x,0,0.3+0.05*sqrt(i));

Nuc_id=[randsample( [1:numel(P)] , 16, true, P(:))]’;

[row,col]=ind2sub(size(P),Nuc_id);

P=f*y.*y’;

hold off

imagesc(P)

hold on

scatter(row,col,10,’r’,’filled’)

pbaspect([1 1 1])

pause(0.1)

end

I have a probability function P that changes over time depending on multiple conditions. In the example code I just changed sigma with the rising index, that should do just fine compared that the original way of calculating P.

Then I take random values from P, based on the probability of P itsself. In this case its 16 random indices.

here, as in my originla Code it works just fine, but in my originla code, after multiple iterations, these 16 rnd indices suddenly stop changing, from one iteration to the next. its like 16 rnd numbers for 200 iterations and suddenly its the same 16 rdn numbers 5 times in a row, sometimes only 15/16 are the same and the one that is different differs just by one, like index 567 instead of 568 or so. Then after 5 times repeating those rnd numbers, all 16 rnd numbers are changed to 16 new rnd numbers, but those also repeat a few times. And after doing this 2 to 4 times, the behaviour stops as sudden as it startet and the rnd numbers are perfectly random again…

I can run the code and visually see when it happens, when I breakpoint the code after "randsample" I can show that the rand number did not change compared to the previous iteration, but then when i manually copy the randsample line inthe command window while the code is paused, the rand numbers are rnd again…

So i realy cant trace back where the problem is comming from, but maybe somebody has an idea.

PS: Not sure if related th this post:

https://de.mathworks.com/matlabcentral/answers/1680149-why-do-the-random-numbers-repeat-in-each-run?s_tid=prof_contriblnk

But I do not have a problem when some numbers or the whole 16 numbers would repeat somewhere or sometimes, but not 5 times in a row out of nothing…

Many thanks in advance

Best regards Hey,

I have a problem with my random numbers that suddenly start to repeat.

Unfortunately I cant chare my code that generates the problem. I scaled it down to something that actually does the same, but somehow the problem does not appear…

But maybe someone can still figure out whats my problem is…

So here is my example code that visuallizes whats going on.

x=-10:0.2:10;

[x1,x2]=meshgrid([-10:0.2:10],[-10:0.2:10]);

close all

figure

xlim([-10 10])

ylim([-10 10])

for i=1:400

y=pdf(‘Normal’,x,0,0.3+0.05*sqrt(i));

Nuc_id=[randsample( [1:numel(P)] , 16, true, P(:))]’;

[row,col]=ind2sub(size(P),Nuc_id);

P=f*y.*y’;

hold off

imagesc(P)

hold on

scatter(row,col,10,’r’,’filled’)

pbaspect([1 1 1])

pause(0.1)

end

I have a probability function P that changes over time depending on multiple conditions. In the example code I just changed sigma with the rising index, that should do just fine compared that the original way of calculating P.

Then I take random values from P, based on the probability of P itsself. In this case its 16 random indices.

here, as in my originla Code it works just fine, but in my originla code, after multiple iterations, these 16 rnd indices suddenly stop changing, from one iteration to the next. its like 16 rnd numbers for 200 iterations and suddenly its the same 16 rdn numbers 5 times in a row, sometimes only 15/16 are the same and the one that is different differs just by one, like index 567 instead of 568 or so. Then after 5 times repeating those rnd numbers, all 16 rnd numbers are changed to 16 new rnd numbers, but those also repeat a few times. And after doing this 2 to 4 times, the behaviour stops as sudden as it startet and the rnd numbers are perfectly random again…

I can run the code and visually see when it happens, when I breakpoint the code after "randsample" I can show that the rand number did not change compared to the previous iteration, but then when i manually copy the randsample line inthe command window while the code is paused, the rand numbers are rnd again…

So i realy cant trace back where the problem is comming from, but maybe somebody has an idea.

PS: Not sure if related th this post:

https://de.mathworks.com/matlabcentral/answers/1680149-why-do-the-random-numbers-repeat-in-each-run?s_tid=prof_contriblnk

But I do not have a problem when some numbers or the whole 16 numbers would repeat somewhere or sometimes, but not 5 times in a row out of nothing…

Many thanks in advance

Best regards randsample, random, not random, repeat MATLAB Answers — New Questions