How can I generate the uniform random array with the constraint of second value not changing more than 20% of the first value
for example (0.5 0.45 0.54 0.63 0.52...........) The difference between first random to second random should be within 20% of first value and the overall random should in uniform manner.
Thank You.

3 Kommentare

Star Strider
Star Strider am 10 Nov. 2017
If you impose constraints, that seems then to make it non-random.
Walter Roberson
Walter Roberson am 10 Nov. 2017
The values after the first are all to be within 20% of the first? Or the values after the first each have to be within 20% of the immediately proceeding value?
htet wai
htet wai am 10 Nov. 2017
I want to generate 1000 random where random(i+1) is greater or less than x% (0-20%) of random(i). but all the 1000 random should be within uniformly distributed.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Nov. 2017

0 Stimmen

V = zeros(1,100);
V(1) = rand;
for K = 2 : 100; V(K) = V(K-1) + (rand() * 2 - 1) * V(K-1) * .2; end

5 Kommentare

htet wai
htet wai am 10 Nov. 2017
it is not uniformly distributing.
V = zeros(1,1000);
V(1) = rand;
for K = 2 : length(V)
rmin = V(K-1)*0.8;
rmax = min(1, V(K-1)*1.2);
V(K) = rand() * (rmax-rmin) + rmin;
end
plot(V)
"it is not uniformly distributing."
Your instructions do not allow it to be uniformly distributed in the short term. The question then becomes whether it can be uniformly distributed in the long term. The answer to that is NO, unless perhaps in the infinite term.
Consider that uniform random on (0,1) can produce 1E-3 or less easily (about 1 time in 1000). On a true random distribution, the very next sample could be close to 1. On your proposed distribution, if you were to take 1E-3 and "randomly" take the full 20% increase, and on the next step "randomly" happen to take the full 20% increase relative to that, and the next step... and so on, then it would take more than 37 consecutive full increases to be able to reach 1 . A 20% loss is harder to make up than a 20% gain is to lose. The result is that the sequence will tend towards 0 and to get stuck near there in any reasonable finite term. Over infinite time it will escape all the way up to near 1 an infinite number of times, but because of the bias, "most" of the infinite time will be spent near 0.
Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan am 10 Nov. 2017
I posted an answer assuming you only wanted the 2nd sample in your sequence to be within 20% of the 1st sample.
If you want consecutive samples to be within 20% of each other, then I agree with the spirit of the comment above. In fact, the sequence will become more non-uniform over time and I think will converge to a delta function after infinite time. So assuming you only want to generate a short sequence (~20-25 samples), your best bet is to run Walter's code by setting the first sample to be equal to one ( V(1) = 1).
Walter Roberson
Walter Roberson am 11 Nov. 2017
Suppose we remove the restriction that the maximum value can be 1. Suppose we simplify the model by alternately adding and subtracting the full 20%, a simple up/down randomness. Then each time we subtract 20% the previous value would be multiplied by 4/5, and each time we add 20% the previous value would be multiplied by 6/5. In this simplified model, those occur in pairs, so each pair would give 4/5 * 6/5 = 24/25 times the result of the previous pair. As you continue this, the end result is going to be (24/25)^n which for large n, the result is clearly going to tend to 0.
htet wai
htet wai am 11 Nov. 2017
Thank you for your answer and I understand your point.
Is it possible to generate the uniform random where the changes between each random term is +/-0.2 (instead of 20% of the first value)?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan am 10 Nov. 2017
Bearbeitet: Kaushik Lakshminarasimhan am 10 Nov. 2017

0 Stimmen

This should work:
N=10; % number of random numbers
success = false;
while ~success
x = rand(N,1);
[minval,minindx] = min(abs(x(2:end) - x(1)));
if minval < 0.2*x(1)
x([2 minindx+1]) = x([minindx+1 2]);
success = true;
end
end
disp(x);
Note that as Star Strider pointed out the sequence will no longer be random.

Kategorien

Mehr zu Random Number Generation finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by