mirror of
https://github.com/anope/anope.git
synced 2026-06-25 22:06:39 +02:00
Use the C++11 random number generator instead of rand().
This is safer, faster, and doesn't require seeding.
This commit is contained in:
+11
-1
@@ -20,7 +20,9 @@
|
||||
#include "sockets.h"
|
||||
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#ifndef _WIN32
|
||||
@@ -745,10 +747,18 @@ Anope::string Anope::Random(size_t len)
|
||||
};
|
||||
Anope::string buf;
|
||||
for (size_t i = 0; i < len; ++i)
|
||||
buf.append(chars[rand() % sizeof(chars)]);
|
||||
buf.append(chars[Anope::RandomNumber() % sizeof(chars)]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
int Anope::RandomNumber()
|
||||
{
|
||||
static std::random_device device;
|
||||
static std::mt19937 engine(device());
|
||||
static std::uniform_int_distribution<int> dist(INT_MIN, INT_MAX);
|
||||
return dist(engine);
|
||||
}
|
||||
|
||||
// Implementation of https://en.wikipedia.org/wiki/Levenshtein_distance
|
||||
size_t Anope::Distance(const Anope::string &s1, const Anope::string &s2)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user