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

1 visualizzazione (ultimi 30 giorni)
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 Commenti

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 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 Commenti
Walter Roberson
Walter Roberson il 8 Mar 2022
Right, I was trying to explain that at https://www.mathworks.com/matlabcentral/answers/1665284-how-to-generate-next-random-number-which-is-30-of-previous-random-number#comment_2024589
Walter Roberson
Walter Roberson il 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".

Accedi per commentare.


KSSV
KSSV il 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 Commenti
Rik
Rik il 7 Mar 2022
Let's first try with one number:
a=66;
f=0.30;%fraction: 30%
You can later replace this with the code that determines the value of this first number.
If you want to merge the branches, you need to use rand only once:
rng(1); % generate the same value
x=double(rand<0.5); % this will be 1 or 0
Since the difference is the sign, we must find a way to leave 1 as 1 and convert 0 to -1. Easy enough:
x=x*2; % will be 2 or 0
x=x-1; % will be 1 or -1
Now you can multiply your sign with the percentage:
x=x*f
x = 0.3000
Next you can multiply that with your initial number to get the delta and add it to the initial number to get the new number.
a+x*a % equivalent to 1*a+x*a=(1+x)*a
ans = 85.8000
All in one line that would be this:
rng(1); % generate the same value
(1+((double(rand<0.5)*2)-1)*f)*a
ans = 85.8000
Now your next step is to generate a number between 0 and 30.
Alternatively you could shorten and simplify this by generating a number between -30 and 30.
Both should be easy now I showed you the idea behind it.

Accedi per commentare.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by