mercredi 1 octobre 2014

Avoid picking the same random word in a list until the list is exhausted


Vote count:

0




This function picks a random word without repeating a word twice in a row.


How can I improve it so that it doesn't pick the same word until the list is exhausted, i.e. it's gone through the whole list?



animateRandomWords() {
List words = ['changemakers', 'community organizers', 'lovers', 'friends', 'doers', 'movers and shakers'];
var rnd = new Random();
randomWord = words[rnd.nextInt(words.length)];
Timer timer = new Timer.periodic(new Duration(seconds: 4), (f) {
HtmlElement el;
el = $['random-word'];
el.style.opacity = '0';
new Timer(new Duration(milliseconds: 750), () {
el.style.opacity = '1';
var newWord;
do {
newWord = words[rnd.nextInt(words.length)];
} while (newWord == randomWord);
randomWord = newWord;
});
});


asked 1 min ago







Avoid picking the same random word in a list until the list is exhausted

Aucun commentaire:

Enregistrer un commentaire