How to generate next random number which is +-30% of previous random number

6 views (last 30 days)
Hello, the following code generate random number depend on levels
i want to generate next random number which is +-30% of previous random number
for example i have random number 66 the next random number should be +-30% of 66
how can i do it in matlab
levels=round(rand*10)+2;
RandomNumber= round(rand(1,levels)*998)+2;
  3 Comments

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 7 Mar 2022
You can see from this simulation that it may peak higher than it started, but over the long term it is going to fade to near 0.
current = 1000;
N = 250;
for K = 2 : N
current(K) = current(K-1) + (rand*.6-.3)*current(K-1);
end
plot(current)
  3 Comments
Walter Roberson
Walter Roberson on 8 Mar 2022
Suppose we were always generating the extreme values, always multiplying an existing value by 7/10 or by 13/10 . Multiplication is commutative, so to calculate the final result for this (restricted) case, we only need to know how many of each of two possibilities we had. U for up, D for down:
syms U D
assumeAlso(U>=0)
assumeAlso(D>=0)
result = sym(7/10)^D * sym(13/10)^U
result = 
which is obviously
7^D*13^U/10^(U+D)
ans = 
Now for the case where U == D
combine(subs(result, U, D))
ans = 
and you can see that trends downwards.
solve(result == 1, U)
ans = 
vpa(ans)
ans = 
You would need more than 4/3 times as many Up events as Down events just to break even over the long term.
Suppose you got lucky and got 10 "Up" in a row at the start. How many "Down" would be required to get back to the original?
vpa(solve(sym(7/10)^D * sym(13/10)^10 == 1))
ans = 
7.3558367058314730861171633806784
Not even 8.
If you are at a Casino and you are given to chance to play this gambling game, you should decline: these are terrible odds, much worse than typical "house odds".

Sign in to comment.


KSSV
KSSV on 7 Mar 2022
a = 66 ; % present random number
% generate next random number +-30 of a
% choose the sign randomly
if rand > 0.5
b = 66*30/100 ;
else
b = -66*30/100 ;
end
  6 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by