A while ago, I came on this forum with a problem with C code. I was trying to write a simple implementation for Mersenne Twister. The code that I had written in C worked fine but I was going for object-oriented model, so I wrote this implementation in C++.
I want the experts on this forum to review the code. It is very basic, beginner level, so there is a lot of room for improvement. Please discuss how you think it can or should be improved.
Also, I have some performance-specific queries that I hope you can answer:
1- It is obviously not portable. How do I make it portable?
2- What if I had to compile a dynamic library (dll / so). How would I go about that? And for that matter, how would I access it in other C++ (or any other language) programs?
3- What exactly is thread-safety and how do I make it thread safe, if that is possible?
4- I want to calculate performance, as in how many numbers can be generated per second. How is that possible? (I have experimented with QPC but the results are unpredictable.)
5- How do I improve performance, make it faster?
On a side note, has anyone in here explored the application of Mersenne Twister in communication applications like encryption key generation or coded multiplexing? I read somewhere that MT is not safe for encryption. But the benefit of the object-oriented model is that I can have multiple streams from multiple generator-objects and mix (xor or multiply) them to get an encryption-safe code-generation algorithm or something. I don't know. I was looking for an expert opinion.
Thank you guys for your time.
EDIT: I meant to add comments so it's easy to understand the code. But I'm sorry that I couldn't. For now, you're going to have to read it like that. Maybe in a day or two, I'll add the commented version of the code.
EDIT: Made the questions bold, so they are easier to spot. :)
You see that I have several functions for returning different types of values. I want to define a function with an arbitrary return type... Meaning, to obtain values of different types, the user can call the same function...
Right now, the function call goes like this:
1 2 3 4 5 6
MersenneTwister mTwist(0xabcdef);
unsignedint i = mTwist.extInt32();
unsignedshort s = mTwist.extInt16();
unsignedchar c = mTwist.extInt8();
double d1 = mTwist.extNormalDbl();
double d2 = mTwist.extSymmetricDbl();
If I use templates, I can make function calls like this:
1 2 3 4 5 6
MersenneTwister mTwist(0xabcdef);
unsignedint i = <unsignedint>mTwist.ext();
unsignedshort s = <unsignedshort>mTwist.ext();
unsignedchar c = <unsignedchar>mTwist.ext();
double d1 = <double>mTwist.ext();
double d2 = <double>mTwist.ext();
Now, the function mTwist.ext performs different operations depending on the return type. For example, for unsignedshort, it extracts a 32-bit number and truncates it using modulus. For double it divides by the maximum possible value of an unsigned 32-bit integer.
How would I be able to determine the return-type in order to be able to perform type-specific operations on it?
EDIT: I don't know why no one has replied yet. I have searched and found several posts on this forum about the Mersenne Twister (however, they don't address the same problems as this one). I would presume many people here are familiar with the topic. But this is like an obsession of mine. I wouldn't be able to continue my routine work if I don't solve this quickly... :)