-1

My code is failing as to not recognize "generator", but I declare it at the top of my C++ file. I need it to be thread-safe, so I declared it with "thread_local":

#include <windows.h>
#include <iostream>
#include <random>

using namespace std;
thread_local mt19937 generator;

DWORD GenerateRandomNumber(DWORD dwMin, DWORD dwMax)
{
uniform_int_distribution <DWORD> distribution(dwMin, dwMax);
return distribution(generator);
}

int main() 
{
random_device rd;
generator(rd());
}
pjs
  • 18,696
  • 4
  • 27
  • 56
JeffR
  • 765
  • 2
  • 8
  • 23

1 Answers1

-3

mt19937::operator() takes no parameters.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • But I need to seed it. The MS example on shows how to do it. – JeffR Mar 24 '19 at 03:38
  • Well, it has a method named - wait for it - `seed()` – Igor Tandetnik Mar 24 '19 at 04:49
  • Asking for help in how to code this mt19937 so it's thread-safe and just gettting smart-alek answer. And a -1 because someone doesn't like the question asked? – JeffR Mar 24 '19 at 11:09
  • There are no threads in the code shown. And the question didn't mention seeding - just asked why the code shown didn't compile, which I answered. In any case, I'm not sure I understand the nature of the difficulty. Now that you know how to seed the generator, just do that at the start of every thread that is going to use it. – Igor Tandetnik Mar 24 '19 at 13:59