random is now really rendom

This commit is contained in:
Senad Uka
2015-10-01 16:10:41 +02:00
parent c75b112027
commit 2c19f97971
2 changed files with 5 additions and 39 deletions

View File

@@ -31,28 +31,11 @@ var RandomItems = React.createClass({
getRandomItems: function() {
var NUMBER_OF_SUGGESTED_ITEMS = 8;
var allItems = ItemStore.getItems();
var randomIndexStart = Math.floor(Math.random() * ((allItems.models.length - NUMBER_OF_SUGGESTED_ITEMS + 1)));
allItems.models = this.shuffle(allItems.models).slice(randomIndexStart, randomIndexStart + NUMBER_OF_SUGGESTED_ITEMS);
allItems.reset(allItems.shuffle(), {silent:true});
allItems.reset(allItems.first(NUMBER_OF_SUGGESTED_ITEMS), {silent:true});
return allItems;
},
shuffle: function(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
},
_onChange: function () {
this.setState({

File diff suppressed because one or more lines are too long