I've read a few posts that deal with higher probabilities and bell curves for selecting elements from an array of numbers, however I have an array of strings I want to choose from. I have a bunch of DIVs on my site that I want to randomly color from a set of 6 colors:
var colors = new Array('red', 'orange', 'yellow', 'green', 'blue', 'purple');
Let's say I really love the color red. How can I tweak the following code to grab a "random" element, but have 'red' be favored over the others? i.e., I want it to return 'red' most of the time.
$('.box').each(function() {
// Get a "random" color
var randomColor = colors[Math.floor(Math.random()*colors.length)];
$(this).css('background-color', randomColor);
});
I realize I could add 'red' a bunch of times to the array, but I'm trying to avoid this. Or is there a different approach altogether? Thanks!