Random Number generator does not refresh for each iteration of the loop due to operation is done on it
16 views (last 30 days)
Show older comments
I want the loop to run the rand function until while condition is met. The moment you have multiple operations on the rand function the code just breaks and the loop just doesnt work properly. Took me awhile to pinpoint the problem. When I change it from C*(1-cos(rand*pi)) to B*(1-cos(rand*pi)), the loop works but doesnt display the right results.
the only reason I could come up with would be, B is a number of its own while C is B with something else which somehows break the rand function to properly run in the while loop until conditions are met.
Part shown here is the problem:
while B > A
C=B/(m0*c2);
B = B / (1+(C*(1-cos(rand*pi))));
s = s + 1;
end
Complete program:
clc
clear
clearvars
%Constants%
%=========%
h=6.6261e-34;
c=2.9979e8;
c2=c^2;
m0= 9.10956e-31;
%Variables%
%=========%
A=15.000e3;
n=100000;
D=224.060e3;
Energydist= normrnd(D,10000,[1,n]);
Sc=zeros(1,n);
Ef=zeros(1,n);
%===========================================================%
for i=1:n
B=Energydist(i);
s=0;
while B > A
C=B/(m0*c2);
B = B / (1+(C*(1-cos(rand*pi))));
s = s + 1;
end
Sc(i)= s;
Ef(i)= B;
end
MaxScattered=(Energydist/A);
Scattered = sum(Sc)
Scattering_efficiency = Scattered / n
tiledlayout(2,2)
ax1=nexttile;
histfit(Sc,50,'poisson')
xlabel('Number of Maximum Scatters Achieved')
ylabel('Frequency')
ax2=nexttile;
histfit(MaxScattered,50,'poisson')
xlabel('Maximum Number of scatter expected')
ylabel('Frequency')
ax3=nexttile;
plot(Ef,Sc)
xlabel('Final Energy of electron (eV)')
ylabel('Number of scatter')
ax4=nexttile;
plot(Energydist,MaxScattered)
xlabel('Initial Energy of electron (eV)')
ylabel('Maximum Number of scatter expected')
1 Comment
Voss
on 17 Dec 2021
What is this code supposed to be doing, and what are the right results?
I don't think the problem is the usage of rand. Also, whether I use C*(1-cos(rand*pi)) or B*(1-cos(rand*pi)), the loop runs, though it may just iterate once for each i.
Answers (1)
Walter Roberson
on 17 Dec 2021
D=224.060e3;
Energydist= normrnd(D,10000,[1,n]);
Those random values are on the order of 224000. Those become the initial values of B.
You calculate
C=B/(m0*c2);
B = B / (1+(C*(1-cos(rand*pi))));
and you want to compare the result to A.
For the moment, call cos(rand*pi) by the name R. cos(rand*pi) (here named R) will be a value between -1 and +1 (theoretically exclusive on both sides because rand can never exactly equal 0 or 1)
So in order to loop again for the moment we have
C=B/(m0*c2);
A == B / (1+(C*(1-R)))
which is the condition
-B/((79228162514264337593543950336*B*(R - 1))/6486512639245165 - 1) == 15000
or
-B/(12214292474343.21*B*(R - 1) - 1) == 15000
when you restrict R (the value of the cos(pi*rand)) to the range 0 to 1, this has a solution only if B is in the range of -8E-11 to -4E-14 . For all positive B, the new calculated value for B is far far less than A, so the loop stops.
Please note that your C coefficient will almost certainly be so large that (C*(1-cos(rand*pi)) is about 1e+18, at which point the difference between representable floating point numbers is in the range of 512 or at least 256 -- so the 1+ is probably not going to make any difference to the calculation.
No "mistake" or malfunction of rand() is needed: your equations just work out that after 1 round, the new B value is going to be so much smaller than A that the loop will stop.
6 Comments
Walter Roberson
on 17 Dec 2021
Okay, but that does not mean that there is a problem with rand()
- you might have a problem with your constants
- your formulas for the calculation might be wrong
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!