I am asking this question for my JS code, but it's actually a problem that I'm having in every programming language.
Let's say I have 5 names for dogs in my simulation, I can hold them in an array and choose a random name for every dog like this
var NAMES = ["Scafy","Shimshon","WoofWoof","Mitzy","AnotherDogName"]//possible names for the dogs
dog.name = NAMES[Math.floor(Math.random()*NAMES.length)] // chooses a random name from for the dog
So this is simple. but my problem is when I want some of the sames to have higher or lower chance, with 5 names is not that difficult to do this:
give WoofWoof much higher chance of getting chosen than the other names by doing this ugly mess:
var NAMES = ["Scafy","Shimshon","WoofWoof","Mitzy","AnotherDogName"]//possible names for the dogs
function choose(){
var random = Math.random()*100
var name = NAMES[0]
if (random>20)
name = NAMES[1]
if (random>30)
name = NAMES[2]
if (random>80)
name = NAMES[3]
if (random>90)
name = NAMES[4]
return name
}
This method works, but it's ugly, and becomes unbelievably complicated when there are a lot of options to choose from and many different chances.
I am sure than I am not the only one who had encounter this problem, and I feel like I'm missing a very elegant solution that hides just below my nose (I hope this sentence makes sense in english too)
This is a general question, I asked it in JS, but I have the same problem in any programming language.
My question is what's the most elegant way of doing it
Thank you