Random Number Generator with range between numbers

6 ビュー (過去 30 日間)
Bryce Schuebert
Bryce Schuebert 2020 年 2 月 14 日
コメント済み: Bryce Schuebert 2020 年 2 月 15 日
I am trying to create a realistic model of traffic conditions, and I want to create a random velocity generator to model this. Problem is, with a range of 0 to 50 MPH, the random numbers can vary wildly between this range between the iterations of the while loop (I am using each iteration as a second). I would like to create a range inside a range that would limit the next random number from being too far away from the previous one. Buses, for instances, might be able to accelerate from 0 - 10 MPH in a few seconds, but not from 0 - 50 MPH.
i = 0
sigma = 0.125 % % of the instant. vel.
t = 0
V_t_old = 5
x_1 = []
x_2 = []
x_3 = []
% Passenger weight
prompt = 'Number of iterations '
pass_count = input(prompt) % This will be coming from the pass counter from Qlik
wv = [] % weight vector
for n = 1:pass_count
S_t = randi([0 40]) %instaneous velocity (random) MPH
V_t = (sigma*S_t)+(1-sigma)*V_t_old
wv(n) = V_t
V_t_old = V_t
if V_t <= 40 && V_t >= 30
x_3(n) = V_t
elseif V_t < 30 && V_t >= 20
x_2(n) = V_t
elseif V_t < 15 && V_t > 0
x_1(n) = V_t
end
end
plot(wv)
format longG

採用された回答

Sindar
Sindar 2020 年 2 月 14 日
instead of (weighted) averaging your old velocity and a new random velocity, just modify your old velocity by a smaller random number, e.g.:
max_accel = 10;
...
% V_t random in range (V_t - max_accel, V_t + max_accel)
V_t = V_t_old + 2*max_accel*(1-rand(1));
% don't let the velocity be negative
V_t = min(0,V_t);
  1 件のコメント
Bryce Schuebert
Bryce Schuebert 2020 年 2 月 15 日
This is great! Easy enough, guess my brain is running slower. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by