Random Number Generator with range between numbers

16 vues (au cours des 30 derniers jours)
Bryce Schuebert
Bryce Schuebert le 14 Fév 2020
Commenté : Bryce Schuebert le 15 Fév 2020
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

Réponse acceptée

Sindar
Sindar le 14 Fév 2020
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 commentaire
Bryce Schuebert
Bryce Schuebert le 15 Fév 2020
This is great! Easy enough, guess my brain is running slower. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by