summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHamuko <hamuko@burakku.com>2016-10-06 02:11:22 +0300
committerAndrzej Rybczak <electricityispower@gmail.com>2016-10-30 17:04:51 +0100
commitd544b777c3ae7e0caa2040f3896bba3ffbd95d34 (patch)
tree390e640ce813c2db1a6f8f41d21d5bc248abb65b
parentcfce9aed08dbe9b2bd28c564ea336157b4f4dd17 (diff)
Use mt19937 random number generator instad of the default one
-rw-r--r--src/actions.cpp12
-rw-r--r--src/global.cpp2
-rw-r--r--src/global.h5
-rw-r--r--src/mpdpp.cpp8
-rw-r--r--src/mpdpp.h5
-rw-r--r--src/ncmpcpp.cpp4
-rw-r--r--src/sort_playlist.cpp2
7 files changed, 26 insertions, 12 deletions
diff --git a/src/actions.cpp b/src/actions.cpp
index e14cbcad..b94c35bd 100644
--- a/src/actions.cpp
+++ b/src/actions.cpp
@@ -2124,11 +2124,15 @@ void AddRandomItems::run()
Statusbar::put() << "Number of random " << tag_type_str << "s: ";
number = fromString<unsigned>(wFooter->prompt());
}
- if (number && (rnd_type == 's' ? Mpd.AddRandomSongs(number) : Mpd.AddRandomTag(tag_type, number)))
+ if (number > 0)
{
- Statusbar::printf("%1% random %2%%3% added to playlist",
- number, tag_type_str, number == 1 ? "" : "s"
- );
+ bool success;
+ if (rnd_type == 's')
+ success = Mpd.AddRandomSongs(number, Global::RNG);
+ else
+ success = Mpd.AddRandomTag(tag_type, number, Global::RNG);
+ if (success)
+ Statusbar::printf("%1% random %2%%3% added to playlist", number, tag_type_str, number == 1 ? "" : "s");
}
}
diff --git a/src/global.cpp b/src/global.cpp
index 9bd2c590..96ed30ab 100644
--- a/src/global.cpp
+++ b/src/global.cpp
@@ -38,4 +38,6 @@ bool SeekingInProgress = false;
std::string VolumeState;
boost::posix_time::ptime Timer;
+std::mt19937 RNG;
+
}
diff --git a/src/global.h b/src/global.h
index ed1f3cf9..2b2a8c59 100644
--- a/src/global.h
+++ b/src/global.h
@@ -22,6 +22,8 @@
#define NCMPCPP_GLOBAL_H
#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <random>
+
#include "mpdpp.h"
#include "screen.h"
@@ -58,6 +60,9 @@ extern std::string VolumeState;
// global timer
extern boost::posix_time::ptime Timer;
+// global RNG
+extern std::mt19937 RNG;
+
}
#endif // NCMPCPP_GLOBAL_H
diff --git a/src/mpdpp.cpp b/src/mpdpp.cpp
index 098bf82e..91bc059e 100644
--- a/src/mpdpp.cpp
+++ b/src/mpdpp.cpp
@@ -561,7 +561,7 @@ void Connection::Add(const std::string &path)
}
}
-bool Connection::AddRandomTag(mpd_tag_type tag, size_t number)
+bool Connection::AddRandomTag(mpd_tag_type tag, size_t number, std::mt19937 &rng)
{
std::vector<std::string> tags(
std::make_move_iterator(GetList(tag)),
@@ -570,7 +570,7 @@ bool Connection::AddRandomTag(mpd_tag_type tag, size_t number)
if (number > tags.size())
return false;
- std::random_shuffle(tags.begin(), tags.end());
+ std::shuffle(tags.begin(), tags.end(), rng);
auto it = tags.begin();
for (size_t i = 0; i < number && it != tags.end(); ++i)
{
@@ -588,7 +588,7 @@ bool Connection::AddRandomTag(mpd_tag_type tag, size_t number)
return true;
}
-bool Connection::AddRandomSongs(size_t number)
+bool Connection::AddRandomSongs(size_t number, std::mt19937 &rng)
{
prechecksNoCommandsList();
std::vector<std::string> files;
@@ -609,7 +609,7 @@ bool Connection::AddRandomSongs(size_t number)
}
else
{
- std::random_shuffle(files.begin(), files.end());
+ std::shuffle(files.begin(), files.end(), rng);
StartCommandsList();
auto it = files.begin();
for (size_t i = 0; i < number && it != files.end(); ++i, ++it)
diff --git a/src/mpdpp.h b/src/mpdpp.h
index 0ed6e283..6eb13a49 100644
--- a/src/mpdpp.h
+++ b/src/mpdpp.h
@@ -23,6 +23,7 @@
#include <cassert>
#include <exception>
+#include <random>
#include <set>
#include <vector>
@@ -524,8 +525,8 @@ struct Connection
int AddSong(const std::string &, int = -1); // returns id of added song
int AddSong(const Song &, int = -1); // returns id of added song
- bool AddRandomTag(mpd_tag_type, size_t);
- bool AddRandomSongs(size_t);
+ bool AddRandomTag(mpd_tag_type, size_t, std::mt19937 &rng);
+ bool AddRandomSongs(size_t number, std::mt19937 &rng);
void Add(const std::string &path);
void Delete(unsigned int pos);
void PlaylistDelete(const std::string &playlist, unsigned int pos);
diff --git a/src/ncmpcpp.cpp b/src/ncmpcpp.cpp
index 111b3e82..7a4a1668 100644
--- a/src/ncmpcpp.cpp
+++ b/src/ncmpcpp.cpp
@@ -89,7 +89,6 @@ int main(int argc, char **argv)
using Global::VolumeState;
using Global::Timer;
- srand(time(nullptr));
std::setlocale(LC_ALL, "");
std::locale::global(Charset::internalLocale());
@@ -131,6 +130,9 @@ int main(int argc, char **argv)
// initialize global timer
Timer = boost::posix_time::microsec_clock::local_time();
+
+ // initialize global random number generator
+ Global::RNG.seed(std::random_device()());
// initialize playlist
myPlaylist->switchTo();
diff --git a/src/sort_playlist.cpp b/src/sort_playlist.cpp
index b9d73a53..ba98a96b 100644
--- a/src/sort_playlist.cpp
+++ b/src/sort_playlist.cpp
@@ -197,7 +197,7 @@ void SortPlaylistDialog::sort() const
quick_sort = [this, &song_cmp, &quick_sort, &iter_swap](Iterator first, Iterator last) {
if (last-first > 1)
{
- Iterator pivot = first+rand()%(last-first);
+ Iterator pivot = first+Global::RNG()%(last-first);
iter_swap(pivot, last-1);
pivot = last-1;