Array.getRandoms()
Gets a specific number of random elements from an array
HTML
<div>
<input id="num" type="text" value="3" />
<a id="pick" href="#">random elements</a>
</div>
CSS
input {
width: 20px;
}
div {
margin-bottom: 10px;
}
JavaScript
var stuff = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Array.implement({
getRandoms: function(limit){
var rand = [];
// clone, and leave the original array in peace
var clone = this.slice(0);
for(var i = 0; limit > i; ++i){
var r = clone.getRandom();
clone.erase(r);
rand.push(r);
}
return rand;
}
});
$('pick').addEvents({
click: function(e){
e.stop();
new Element('p', {
text: stuff.getRandoms($('num').get('value')).join(', ')
}).inject($(document.body));
}
});