Random numbers from randsample suddenly repeating
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Harald
2024-7-4
编辑:Harald
2024-7-4
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.
A nice read on surprisingly repeating random numbers: https://blogs.mathworks.com/matlab/2022/06/07/6-3-7-8-5-1-2-4-9-10-or-a-story-of-surprise-about-randomness/
If none of this helps, please try to come up with sample code that exhibits the problem.
Best wishes,
Harald
回答(1 个)
Garmit Pant
2024-7-4
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:
- Refer to this page to understand different random stream generation methods: https://www.mathworks.com/help/releases/R2022b/matlab/ref/randstream.html
I hope you find the above explanation and suggestions useful!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!