Browse Source

Use RND class for shrink_to_rand_elem and add assertions

fixes
Antonio SJ Musumeci 5 days ago
parent
commit
24057f741b
  1. 4
      src/rnd.cpp
  2. 8
      src/rnd.hpp

4
src/rnd.cpp

@ -20,6 +20,8 @@
#include "rapidhash/rapidhash.h"
#include <cassert>
#include <sys/time.h>
@ -57,6 +59,7 @@ RND::rand64(void)
u64
RND::rand64(cu64 max_)
{
assert(max_ > 0);
return (RND::rand64() % max_);
}
@ -64,5 +67,6 @@ u64
RND::rand64(cu64 min_,
cu64 max_)
{
assert(max_ > min_);
return (min_ + (RND::rand64() % (max_ - min_)));
}

8
src/rnd.hpp

@ -20,7 +20,7 @@
#include "base_types.h"
#include <random>
#include <vector>
class RND
{
@ -38,14 +38,10 @@ public:
void
shrink_to_rand_elem(std::vector<T> &v_)
{
static std::mt19937 gen(std::random_device{}());
if(v_.size() <= 1)
return;
std::uniform_int_distribution<> dist(0,(v_.size() - 1));
std::swap(v_[0],v_[dist(gen)]);
std::swap(v_[0],v_[RND::rand64(v_.size())]);
v_.resize(1);
}

Loading…
Cancel
Save