random.h
#ifndef RANDOM_H
#define RANDOM_H
#include <random>
#include <ctime>
class Random
{
public:
explicit Random(QObject* parent = nullptr);
int random(int min, int max);
double random(double min, double max);
private:
std::default_random_engine m_engine;
};
#endif // RANDOM_H
random.cpp
#include "random.h"
Random::Random() : m_engine(time(nullptr))
{
}
int Random::random(int min, int max)
{
std::uniform_int_distribution<> dis(min, max);
int ret = dis(m_engine);
return ret;
}
double Random::random(double min, double max)
{
std::uniform_real_distribution<> dis(min, max);
double ret = dis(m_engine);
return ret;
}