1

Here is a challenge for me, How can I code this properly? I really need some help to deal with it...

It's about weighted - randomizing actually. I try to explain this step by step with my bad English :) Sorry if any unnecessary information is given here, I'll be glad if anyone can make this simpler...

Let's say we want to randomly pick a city name based on it's desired weight.

Here we have an array of objects containing those city names and our sample desired weight and each city distance from the west.

var list = [
   { name: "New York", distance: 4, weight: 1 },
   { name: "Atlanta", distance: 3, weight: 1 },
   { name: "Dallas", distance: 2, weight: 1 },
   { name: "Los Angeles", distance: 1, weight: 1 },
];   

So New York is far and Los Angeles is near. Ok now, I can pick a city based on its weight using this function (It's not necessary to show you the code behind this but feel free to ask for it if you want to take a look at it).

var random_item = getRandomItem(name, weight);
console.log(random_item);

The result of this code is one of those cities, for example New York.

My problem is: after picking up the very first city, I want a new array of modified weights in order to:

A) give the already picked up city weight of 0 (this way it can not repeat again Note: this is not the problem).

B) modify the weight of remainder cities based on their distance from the last picked up city with this logic: as much as cities are far from last picked up city they get more weight. (in another word I want to pick up cities based on as far as they can be form last city that already is picked up...)

For instance: if New York is the first picked up city, then the New York weight should be 0, and as the distance from Los Angeles is 3 (New York.distance: 4 vs Los Angeles.distance: 1) add Los angles.weight a 3 and make it 4.

Any idea or solution is appreciated...

1
  • Your logic won't work as you desire. Take Atlanta as the random city: Atlanta weight becomes: 0, New York weight becomes: (3 - 4) + 1 = 0 - you have two with Zero weights given your logic. I recommend not adding the current weight. that will provide a signed value as a weight that cannot repeat. Commented Jun 30, 2019 at 17:09

1 Answer 1

3

Here is an answer that reflects my comment:

Your logic won't work as you desire. Take Atlanta as the random city: Atlanta weight becomes: 0, New York weight becomes: (3 - 4) + 1 = 0 - you have two with Zero weights given your logic. I recommend not adding the current weight. that will provide a signed value as a weight that cannot repeat.

This answer will ensure unique values for the weight property. You can think of negative values moving Westerly while positive values move Easterly from the Zero weighted location.

var list = [
   { name: "New York", distance: 4, weight: 1 },
   { name: "Atlanta", distance: 3, weight: 1 },
   { name: "Dallas", distance: 2, weight: 1 },
   { name: "Los Angeles", distance: 1, weight: 1 },
];


// Given random city is Atlanta
let randomCity = "Atlanta";
let baseDistance = list.find(l => l.name == randomCity).distance;

let newList = list.map(c => {
  console.log();
  let newWeight = Math.abs(baseDistance - c.distance);
  return {
    name: c.name,
    distance: c.distance,
    weight: newWeight
  };
});

console.log(newList);

EDIT: Based upon the OP's comment, and my apparent misunderstanding of what this way it can not repeat again actually means (I thought uniqueness was the idea), I've changed the weight to an absolute value.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer ... plus one... but one thing to mention: I just care about the Absolute value of the differences between city distances. if given random city is Atlanta then New York and Dallas should be given the equal weight because thier Absolute value distance difference from Atlanta is 1.
Then this: this way it can not repeat again from your question has confounded my understanding of your question. Can the values repeat or not?
Sorry, For the misunderstanding that my poor English caused... The picked city/cities won't be picked up again as the weight of it/them is 0. after picking up Atlanta the only choices are the reminder cities...
Instead of filter you can use find which will return the object instead of fetching from array of zero to get the baseDistance
@DILEEPTHOMAS - sure, probably a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.