If $(\'.my-element\')
matches multiple element, is there a quick way to get a random element out of these?
var numElements = $('.my-element').length;
var randomNum = Math.floor(Math.random()*numElements);
//Select your random element
$('.my-element:nth-child(' + randomNum + ')');
To get an even distribution, you can multiply random by the array count and drop the decimal with the bitwise operator.
var arr = ['a','b','c'];
arr[~~(Math.random() * arr.length)]; //even odds of a, b, or c
$.fn.random = function() {
return this.eq(Math.floor(Math.random() * this.length));
}
$(selector).random();