Hi, I havea simulation in which cars are moving. The point is that a car has a start point (coordinates x and y) and an end point (coordinates x1 and y1). I'd like to make sure that the distances that the cars need to travel are following a gaussian distribution with mean 50 and standard deviation 30. I can't figure out the logic by which I can do this so that the cars are placed randomly on a grid of A by A and all have various places to travel to.

Thanks,

The Random class has a nextGaussian() method, so that's obviously going to be part of yur solution...
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextGaussian%28%29

Wikipedia has a list of methods for generating normally-distributed values. Note that the methods described correspond to a standard normal distribution, but the linked article gives a simple transformation from a standard normal …

Oops, missed the `nextGaussian` method. That's certainly easier. You can use the same transformation from standard to custom normal distributions.

Maybe allocate starting x and y at random, then chose the end x and y as (start x) + (suitable gaussian), (start y) + (suitable gaussian).

## All 13 Replies

Thanks - I know about nextGaussian(), the main problem is figuring out how to randomly assign them the start and stop locations while using the Gaussian distribution.

Oops, missed the `nextGaussian` method. That's certainly easier. You can use the same transformation from standard to custom normal distributions.

Maybe allocate starting x and y at random, then chose the end x and y as (start x) + (suitable gaussian), (start y) + (suitable gaussian).

Thanks! Any thoughts on how to work the logic on the placement of these vehicles?

Just a random x between 0 and (width of space) - linear distribution, ditto y?

Are the cars supposed to travel along the grid, or do they go directly from point A to point B?

You're about to point out that doing pythagorus on two gaussians doesn't give a gaussian hypotenuse?

The cars go along the grid (so distance traveled = change in x + change in y).

i tried this...but obviously it doesn't work for what i want it to do....

``````for (int i = 0; i < Math.random() * 10; i++) {
int mean = 200;
int stddev = 30;
Random r = new Random();
int startX = (int)(r.nextGaussian() * stddev + mean);
int startY = (int)(r.nextGaussian() * stddev + mean);
int destinationX = (int)(r.nextGaussian() * stddev + mean);
int destinationY = (int)(r.nextGaussian() * stddev + mean);

....
``````

You're about to point out that doing pythagorus on two gaussians doesn't give a gaussian hypotenuse?

No; it'll give a distribution with a different mean/stdev. So if we're being precise, either pick the x and y deltas using smaller parameters, or pick total distance from the desired distribution and decompose that into x and y lengths.

If the exact distribution isn't critical, I'd go with `(startX + g1, startY + g2)` as JC suggests.

can you help me with the code for that using the logic:
1) randomly select the start point
2) predetermine the distance you need that car to travel using nextGaussian()
3) identify all the end points that are that distance away
4) randomly select one

as you can see, i don't know how to do steps 3 and 4

``````for (int i = 0; i < Math.random() * 10; i++) {
int drivingDistance = 0
int mean = 50;
int stddev = 30;
Random r = new Random();

//randomly pick start points
int startX = (int)(Math.random() * gridWidth);
int startY = (int)(Math.random() * gridHeight);

//pick driving distance
drivingDistance = (int)(r.nextGaussian() * stddev + mean);

//find all possible destinations given start point

//randomly pick one
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.