Random numbers from randsample suddenly repeating

2 views (last 30 days)
Marc Laub on 4 Jul 2024
Answered: Garmit Pant on 4 Jul 2024
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));
P=y.*y';
Nuc_id=[randsample( [1:numel(P)] , 16, true, P(:))]';
[row,col]=ind2sub(size(P),Nuc_id);
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:
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
3 CommentsShow 1 older commentHide 1 older comment
Marc Laub on 4 Jul 2024
Edited: Marc Laub on 4 Jul 2024
Thx, I copy pasted it to a different place, but since P was still in the workspace I still worked. I changed it, example code works now, but my actuall problem still exists.
Harald on 4 Jul 2024
Edited: Harald on 4 Jul 2024
Hi,
since your code does not exhibit the problem, I can only guess:
• Look for uses of rng in your code as this command changes the state of the random number generator.
• Does your code use parallel constructs like parfor or spmd? If so, be careful about using commands like rng("shuffle") inside those, since workers may run these at the same time and thus use the same state of the random number generator.
If none of this helps, please try to come up with sample code that exhibits the problem.
Best wishes,
Harald

Garmit Pant on 4 Jul 2024
Hello Marc Laub
To address the issue of repeating randomly generated numbers using MATLAB function “randsample”, you can utilise the “RandStream” function to generate a new random stream for random number generation.
The MATLAB function “randsample” draws from the default random number stream of the running session if a random number stream is not specified. The following code snippet defines a custom random number stream for the “randsample” function:
Nuc_id = [randsample(RandStream('dsfmt19937','Seed','shuffle'),[1:numel(P)], 16, true, P(:))]';
This method will ensure that a new random stream is generated in each iteration of the loop for the function “randsample” to use.
For further understanding, please refer to the following MATLAB Documentation:
I hope you find the above explanation and suggestions useful!

R2022b

Community Treasure Hunt

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

Start Hunting!