Random Number Generator with range between numbers

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

This is great! Easy enough, guess my brain is running slower. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2020년 2월 14일

댓글:

2020년 2월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by