How to generate my traffic object at random Y and random Lane locations

Viewed 15

I'm a beginner JavaScript student here with a, seemingly, fairly simple question. I need help editing this generateTraffic(N2) function to make the traffic spawn randomly within the range of y values and lane values given. As of now, it works but only slightly. All of the traffic randomly spawns according to these values, but all on top of each other instead of all in separate locations.

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
  }


function generateTraffic(N2){
    const traffic=[];
    const randomY = getRandomInt(-200, -100);
    const computerResponse = getRandomInt(0, 3);
    for(let i =1;i<=N2;i++){
        traffic.push(new Car(road.getLaneCenter(computerResponse),randomY,30,50,"DUMMY",2));
    }
    return traffic;
}

How would I go about editing this function to achieve the many random location spawns?

0 Answers
Related