Use mt19937 random number generator instad of the default one
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,4 +38,6 @@ bool SeekingInProgress = false;
|
||||
std::string VolumeState;
|
||||
boost::posix_time::ptime Timer;
|
||||
|
||||
std::mt19937 RNG;
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user