/**
* Walks linearly through the list to find an element.
* returns true if it's found.
*/
function elementIn(collection, element) {
for (var i=0; i < collection.length; i++) {
if (collection[i]==element) {
return true;
}
}
return false;
}
/**
* Returns a new list of n random elements taken out of myList.
*/
function getNElementsAtRandom(myList, n) {
var toGo = n;
var result = [];
var indexesUsed = [];
while (toGo > 0) {
index=-1;
do {
index = Math.floor(Math.random()*(myList.length));
console.log(index);
} while (elementIn(indexesUsed, index));
indexesUsed.push(index);
result.push(myList[index]);
toGo--;
}
return result;
}
Did this help you?
Tip $1
Tip $2
Tip $5
This entry was posted
on Thursday, January 5th, 2012 at 9:38 pm and is filed under Code.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.