samedi 23 mars 2019

How to seed mt19937 once per app in C and use it multiple times?

I can get the mt19937 rng seeded in a simple app. Now I am trying to get it seeded once per app and use it multiple times whenever it's needed. Here is my code. I can't get it to compile. I'm sure it's something simple I'm overlooking?

And maybe a better way to seed the mt19937?

main.cpp

#include <iostream>
#include "general.h"

int main() {

CallOncePerApp();

// Loop for output testing
// Ultimately, GenerateRandomNumber can be called from anywhere in any CPP file
for (int i=0;i<10;i++)  {
    int dwGen = GenerateRandomNumber(1, 1000);
    cout << dwGen; // print the raw output of the generator.
    cout << endl;
    }
}

general.h:

#include <random>

using namespace std;

extern random_device rd;
extern void CallOncePerApp();
extern mt19937 gen;

extern unsigned int GenerateRandomNumber(unsigned int dwMin, unsigned int dwMax);

general.cpp:

#include <random>

using namespace std;
random_device rd;
mt19937 gen;

void CallOncePerApp() 
{
    gen(rd); // Perform the seed once per app
}

unsigned int GenerateRandomNumber(unsigned int dwMin, unsigned int dwMax) 
{
    uniform_int_distribution <int> dist(dwMin, dwMax); // distribute results between dwMin and dwMax inclusive.
    return dist(gen);
}




Aucun commentaire:

Enregistrer un commentaire