Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
How to get C++ Mersenne Twister 19937 generator to give the same results on Windows and Linux?

We have the following code to generate normally distributed random numbers.

   double average = 10.0; double spread = 4.0;
   std::mt19937 generator;
   generator.seed(1);
   std::normal_distribution<double> distribution(average, spread);

   for (uint i = 0; i < 10; i++)
      cout << "randomNumber=" << distribution(generator) << endl;

This should produce the same exact sequence on Linux and Windows. But it doesn't. The sequences are identical when rerun on the same OS. But the sequences are different when run on Linux vs. Windows. The generators are the same and the seeds are the same, so they should give identical sequences.

What's missing or incorrect that would allow the code to create identical sequences on both operating systems?

Windows 10. Visual Studio 2010.

Ubuntu 14.04. g++ (gcc 4.9.2)

Windows Output:

randomNumber=10.6243
randomNumber=11.2256
randomNumber=7.72784
randomNumber=8.30245
randomNumber=6.77485
randomNumber=9.18181
randomNumber=5.19982
randomNumber=8.28505
randomNumber=5.24899
randomNumber=15.2219

Linux Output:

randomNumber=7.80102
randomNumber=4.38851
randomNumber=16.331
randomNumber=5.81941
randomNumber=11.0304
randomNumber=2.16242
randomNumber=3.96877
randomNumber=8.73883
randomNumber=13.4327
randomNumber=10.2854
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The C++ library specification guarantees that the generator will produce the same sequence of values, but the distributions are not required to.

If you modify your code to produce raw output from the mt19937 generator you will find that the results are the same across platforms:

std::mt19937 generator(1);

for (uint i = 0; i < 10; i++)
  cout << "randomNumber=" << generator() << endl;

It certainly would be useful if the distributions were also deterministic across platforms, but I imagine the committee felt that would take away too much freedom from implementers to implement new algorithms for producing distributions.

Anyway, if you want deterministic distributions you'll need to implement them yourself or use another library that provides deterministic cross-platform implementations.

over 4 years ago · Santiago Trujillo Relatório

0

You can use the boost implementation of the mersenne twister in order to get the same random values on windows and linux.

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>

boost::mt19937 m_mt;
boost::uniform_int<> dist(min, max);
boost::variate_generator<boost::mt19937 &, boost::uniform_int<>> die(m_mt, dist);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda