PDA

View Full Version : shuffle store



nofx
20 Dec 2011, 11:55 PM
I have a store with multiple items models stored inside of it. But now i need a way to randomly get the items out of it. Is there a way to shuffle the store? Or is there perhaps another approach i can use to get the items randomly (without duplicates ofcourse).

mitchellsimoens
21 Dec 2011, 6:45 AM
I would create a random number from 0 to the number of records and get that record. I would keep an Object that contains the already used random numbers. Just typing off the top of my head.


getRandomIndex: function(min, max, used) {
var index = Math.floor(Math.random() * (min - max) + max);

if (used[index]) {
getRandomIndex(min, max, used);
return;
}

return index;
}

var index = getRandomIndex(0, store.getCount(), {}),
rec = store.getAt(index);