+int Randomizer::next() {
+ int res = 0;
+ if (_current == _rand.count()) {
+ _shuffle();
+ _current = 0;
+ res = next();
+ } else {
+ res = _rand.at(_current);
+ }
+ ++_current;
+ return res;
+}
+
+void Randomizer::setPlaylist(QList<int> pl) {
+ _playlist = pl;
+ _current = 0;
+ _shuffle();
+}
+
+void Randomizer::_shuffle() {
+ _rand.clear();
+ // Fisher-Yates algorithm:
+ _rand = _playlist;
+ int cnt = _playlist.count();
+ int j = 0;
+ int tmp = 0;
+ for (int i = cnt-1; i > 0; i--) {
+ j = qrand() % (i+1);
+ tmp = _rand[i];
+ _rand[i] = _rand[j];
+ _rand[j] = tmp;
+ }
+}
+