How to generate next random number which is +-30% of previous random number
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (2 件)
Walter Roberson
2022 年 3 月 7 日
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 件のコメント
Walter Roberson
2022 年 3 月 8 日
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
2022 年 3 月 8 日
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
which is obviously
7^D*13^U/10^(U+D)
Now for the case where U == D
combine(subs(result, U, D))
and you can see that trends downwards.
solve(result == 1, U)
vpa(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))
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".
KSSV
2022 年 3 月 7 日
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 件のコメント
Rik
2022 年 3 月 7 日
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
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
All in one line that would be this:
rng(1); % generate the same value
(1+((double(rand<0.5)*2)-1)*f)*a
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.
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!