# how to compute random??

1 view (last 30 days)
Jason on 26 Sep 2022
Answered: David Hill on 26 Sep 2022
Let Sn be the sum of n such random numbers, and let
An be the average these random numbers. Intuitively, An should be closer to 0.5 when n
becomes larger.
Write a program that inputs an error tolerance tol, computes the smallest integer ns
such that |Ans −0.5| ≤ tol, then displays ns, Rns, and Ans.
But I don't know how you would compute it? It really confuses me, thanks
%b2
tol=input('Enter error tolerance:');
R=0;
n=0;
A=0;
while abs(A-0.5)>=tol
n=n+1;
n=rand;
end
Ans=Rns/ns;
Rns=R ;
fprintf('Rns=%.16e\n',Rns)
fprintf('Ans=%.16e\n',Ans)
fprintf('nStar=%.16e\n',nStar)
dpb on 26 Sep 2022
Well, you've begun a nice structure, but you haven't yet done anything inside the while loop that will change the error -- and the assignment says to use variable Ans for the average of the N random numbers so you really should use that variable name to be fully compliant.
How can you compute an average of N numbers when and then add another when it comes along? Think about what the definition of an average is; should give you an idea from what the calculation is.
It also seems as though you're supposed to save the random numbers generated on the way; you'll need to see about preallocating a sizable array and store into it each pass the new value --
You have one logic error in the above--you started the counter n to count the number of iterations (which was supposed to be variable ns) but then you overwrite it with the random variable...you've got to use another variable (supposed to be Rns) to hold the random variables -- and it needs to be the array mentioned above.
Contratulations on the structure of the code, though, that's well done for newbie...

David Hill on 26 Sep 2022
Run a simulation many times below.
tol=.01;
for k=1:1000 %I ran the simulation 1000 times
n=rand(1,10000);
An=cumsum(n)./(1:10000);
a=abs(An-.5);
f(k)=find(a<=tol,1);
end
nm=mean(f) %I took the average of the lowest n at which abs(An-.5)<=tol
nm = 65.3600

Walter Roberson on 26 Sep 2022
while abs(A-0.5)>=tol
n=n+1;
n=rand;
end
n appears to be your counter. But you are overwriting n with a random number.
You never change A within your while loop so if the loop is entered at all it can never exit.
Ans=Rns/ns;
Rns and ns are not assigned at this point
Rns=R ;
R is assigned at this point but it is still the initial 0 that it was assigned.
Each time you are generating a new random number you should be adding it to a total, and you should be calculating an average (that should probably be assigned into A)
fprintf('Rns=%.16e\n',Rns)
fprintf('Ans=%.16e\n',Ans)
In terms of your code, what are Rns and Ans intended to mean ?? They are not obvious variable names and there is no documentation for them. Will an output about the value of Rns= be meaningful to readers?
fprintf('nStar=%.16e\n',nStar)
nStar has not been assigned to at this point.

### Categories

Find more on Sources in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!