c++randomc++17mersenne-twister

Generate random long long C++


int generator

I currently generate deterministic pseudo-random ints using this code:

#include <chrono>
#include <ctime>
#include <random>
#include <stdint.h>

const uint32_t CurrentTime = static_cast<uint32_t>(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count());

std::mt19937 Mersenne = std::mt19937(static_cast<std::mt19937::result_type>(CurrentTime));

int Min = 3;
int Max = 6;
std::uniform_int_distribution<> Distribution(Min, Max-1);

int Result = Distribution(Mersenne);

The problem

There's two problems with this:

  1. The parameters for Distribution must be ints.
  2. The result from Distribution(Mersenne) is an int.

The question

How do I generate a random long long instead of an int, with the Min and Max parameters also being long longs instead of ints?

The context

I'm creating a deterministic game (peer-to-peer architecture), and the large minimum-size of a long long is needed as a sort of fixed-point number (since floats can cause non-determinism).

I won't accept answers that:

I would much prefer a solution from the standard library if there is one.

Ideally, the solution should be at least as efficient as my existing code on a 64-bit machine.


Solution

  • the documentation says that the template parameter can be long long

    long long Min = 3;
    long long Max = 6;
    std::uniform_int_distribution<long long> Distribution(Min, Max-1);
    
    long long Result = Distribution(Mersenne);