🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

mt19937 in C++

Started by
5 comments, last by Alberth 3 years, 12 months ago

I have a global variable of type mt19937. I am trying to seed it in main(), but every time I run my app, it gives the same pseudorandom numbers. The code I'm using is listed below. Any idea what kind of beginner's mistake that I'm making?

long unsigned int s = time(0);
mt_rand.seed(s);
Advertisement

Try using unsigned int (without the long) s? Otherwise you could show the entire main function.

taby said:

I have a global variable of type mt19937. I am trying to seed it in main(), but every time I run my app, it gives the same pseudorandom numbers. The code I'm using is listed below. Any idea what kind of beginner's mistake that I'm making?

long unsigned int s = time(0);
mt_rand.seed(s);

I've done my random number generation as follows, and please note there might be better or newer ways, but this is what I used a few years back on a card game for getting random numbers.

#include <iostream>
#include <random>
#include <chrono>


int main()
{
	std::mt19937 mt(std::chrono::system_clock::now().time_since_epoch().count());
	std::uniform_int_distribution<unsigned> u(1, 10);
	int randomNumber = 0;

	// Print out 100 Random Numbers between 1 and 10
	for (int a = 0; a < 100; a++)
	{
		randomNumber = u(mt);

		std::cout << randomNumber << std::endl;
	}
}

Programmer and 3D Artist

So, I pared it down to a simple code – and it works. :(

#include <iostream>
#include <ctime>
#include <random>
using namespace std;

mt19937 mt_rand;

int main(void)
{
	mt_rand.seed(static_cast<long unsigned int>(time(0)));

	cout << mt_rand() << endl;

	return 0;
}

Are you by any chance using your random number generator in the constructors of other global variables? Seeding should work, but it obviously only affects random numbers generated after calling the seed function.

Another thing to watch out for is multithreading. It's not safe to use the same random number generator from multiple threads simultaneously.

@taby Your second (working) code isn't exactly the same as the first description. For proper checking, you should make them literally as much the same as possible. You don't want to introduce more sources of confusion until you found the culprit. Alternatively, you could try the working initialization in your non-working version.

Also, draw a random number directly after initializing in the non-working version, like you do in the working version. (Keep things literally equal.)

For debugging, print the seed value that you use for initializing (it should change in time). A second step is to also print the raw drawn numbers everywhere. Since your generator is global, it is initialized before main even starts with some default (probably fixed), and perhaps even used. With all the above output you should be able to see if initializing happens before use.

If that works, perhaps it's your ‘u’ in your non-working version? Split the statement, and print the raw mt sample before using it in ‘u’.

This topic is closed to new replies.

Advertisement