-rw-r--r-- 1622 cryptattacktester-20230614/random.cpp raw
#include <random>
#include "random.h"
using namespace std;
static mt19937_64 rng;
// XXX: could substitute a cryptographic RNG
void random_seed(bigint seed)
{
rng.seed(seed);
}
static uniform_int_distribution<short> choose_bit(0,1);
bool random_bool(void)
{
return choose_bit(rng);
}
static uniform_int_distribution<int> choose_int(
numeric_limits<int>::min(),
numeric_limits<int>::max()
);
int random_int(void)
{
return choose_int(rng);
}
static uniform_int_distribution<unsigned int> choose_uint(
numeric_limits<unsigned int>::min(),
numeric_limits<unsigned int>::max()
);
unsigned int random_uint(void)
{
return choose_uint(rng);
}
static uniform_int_distribution<long> choose_long(
numeric_limits<long>::min(),
numeric_limits<long>::max()
);
long random_long(void)
{
return choose_long(rng);
}
static uniform_int_distribution<unsigned long> choose_ulong(
numeric_limits<unsigned long>::min(),
numeric_limits<unsigned long>::max()
);
unsigned long random_ulong(void)
{
return choose_ulong(rng);
}
static uniform_int_distribution<long long> choose_longlong(
numeric_limits<long long>::min(),
numeric_limits<long long>::max()
);
long long random_longlong(void)
{
return choose_longlong(rng);
}
static uniform_int_distribution<unsigned long long> choose_ulonglong(
numeric_limits<unsigned long long>::min(),
numeric_limits<unsigned long long>::max()
);
unsigned long long random_ulonglong(void)
{
return choose_ulonglong(rng);
}
bigint random_bigint_below(const bigint &m)
{
// XXX: handle larger ranges
bigint result = random_longlong();
return result%m;
}