I'd suggest you select a random number that's from 0 to the total weight of your fish. Then, each fish can have a piece of that random range according to it's weight which gives it the weighting you want. This ends up giving each fish a number of buckets that corresponds to it's weighting value and then you pick a random number from 0 to the total number of buckets and find out who's bucket the random number landed in.
It's the same idea as turning each weighting number into a percentage change and then picking a random number from 0 to 99.
This code should do that:
var FISH = {
"level1": [
//["name", xp, weight]
["Shrimp", 10, 95],
["Sardine", 20, 85],
["Herring", 30, 75],
["Anchovies", 40, 65],
["Mackerel", 40, 55]
]
};
function getRandomFish(level) {
var fishForLevel = FISH[level];
var fishTotalWeight = 0, fishCumWeight = 0, i;
// sum up the weights
for (i = 0; i < fishForLevel.length; i++) {
fishTotalWeight += fishForLevel[i][2];
}
var random = Math.floor(Math.random() * fishTotalWeight);
// now find which bucket out random value is in
for (i = 0; i < fishForLevel.length; i++) {
fishCumWeight += fishForLevel[i][2];
if (random < fishCumWeight) {
return(fishForLevel[i]);
}
}
}