Jump to content
  • Advertisement
Sign in to follow this  
Sammy0037

mersenne twister & boost

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Its late so I must be really dense at the moment. ;o) For the life of me Boosts mersenne twister seems like trying to decipher Greek. And, finding examples of how to use it are eluding me. Many just refer back to the header file. Yes lots of Googling. Actually I tried to get it to work before but gave up in fustration and stuck with the good ol randf() function. Actually Boost smart pointers are very handy. But this one... ugh. Best I come close to is boost::random::UIntType, which if I understand correctly is the template. At the moment all I get are 'expected primary-expression' errors. Maybe its actually correct but this error is pointing to something different? cheers all

Share this post


Link to post
Share on other sites
Advertisement
There is an example on the very first page.

First you create a random number generator object (slightly modified from their example to include a time-based seed).

boost::mt19937 rng(time(0));

Then you create an object that represents the distribution of numbers you want to generate.

boost::uniform_int<> six(1,6);

Then you use them to create an object that uses the RNG to produce a random number following the selected distribution. The template arguments must match the types used for the RNG and distribution objects.

boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(rng, six);

Finally, you use that object to actually get random numbers.

int x = die();

Share this post


Link to post
Share on other sites
Yes I saw that one, which is for integers. And it also works quite nicely where I need those. ;o) Ive been working on variations of it to get the aformentioned mersenne to work for floats.

Was simple to change it to uniform_real, which also works... or I should say there are no compile errors. I have yet to test the results. As many seem to like the other, I thought I would see if I could get it to work. I highly suspect that I am close. Just not close enough.

;o)

Share this post


Link to post
Share on other sites
<snip> I thought it was in Java forum...

You can do this with reasonable wersion of MT:

double random1to0Value = (double) rand.nextInt() / MAX_VALUE_OF_INT;

and if you need float then you can

float floatRandomValue = (float) random1to0Value;

for boost you might like some variation of:

explicit uniform_real(RealType min = RealType(0),
RealType max = RealType(1))

Look into http://www.boost.org/boost/random/uniform_real.hpp


Well this is fairly easy and I hope you didn't attempt to recode a bitshift register based random generator into floats. (You would really hate creating a 4000 bit floating point member, and all that overhead with 4000 bit floating point arithmetic.)

Share this post


Link to post
Share on other sites
At the moment... uniform_real. I think it works right.

[edit]
Maybe uniform real is the only one I need to worry about anyways, and I really am trying to reinvent the wheel.
[/edit]

boost::mt19937 rng;
boost::uniform_real<> ten(1,10);
boost::variate_generator<boost::mt19937&, boost::uniform_real<> >
test(rng, ten); // glues randomness with mapping
int x = test();

testing.... right now Im working on getting it the syntax right. Then I get to figure out what to use instead of all the 1's.

boost::mt19937 rng;
boost::random::mersenne_twister<unsigned int, 1,1,1,1,1,1,1,1,1,1,1,1> one();
boost::variate_generator<boost::mt19937&,
boost::random::mersenne_twister<unsigned int, 1,1,1,1,1,1,1,1,1,1,1,1> >
//syntax ok, or so the compiler says, up to this point

boo(rng, one); //error
float x = boo();

But as you say, perhaps I am making this harder than it needs to be.

[Edited by - Sammy0037 on September 12, 2005 6:38:51 PM]

Share this post


Link to post
Share on other sites
boost::random::mersenne_twister<unsigned int, 1,1,1,1,1,1,1,1,1,1,1,1> one();

Unless you know how the Mersenne Twister works internally and are able to intelligently select new parameters, you shouldn't try to do that: boost::mt19937 is a typedef for an appropriate Mersenne Twister PRNG type.

Share this post


Link to post
Share on other sites
Unless the boost rng library has support for floats, you should select an integral range and map it to your target floating-point range. e.g. make a die that produces numbers from -8388608 to 8388608, then divide the result by 8388608.0f to convert to a number between -1.0 and 1.0 and every bit of precision will be covered.

Share this post


Link to post
Share on other sites
That is true. I have a small function thats supposed to find a random number between two floats... say between 1.45 and 4.11 etc. Basically multiply/expand - get random - then divide. So why go through the trouble of this? My needs at the moment works just fine with this approach. But later the complexity will increase. Better to learn now with a smaller need than a large one.

Last few days Ive been learning how to use Dev-C++. I didnt know pragma was MS specific for example. But today fired up VC6. Seems that it doesnt like some of the Boost libraries. If I am reading correctly, its the use of a template within a template. Or something like that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sammy0037
I didnt know pragma was MS specific for example.


All #pragma are compiler-specific by definition.

Quote:
But today fired up VC6. Seems that it doesnt like some of the Boost libraries.


VC6's template support is limited. Boost uses workarounds in places, but sometimes it Just Cannot Be Done.

Quote:
If I am reading correctly, its the use of a template within a template. Or something like that.


Template-template parameters aren't supported by VC6. You can still use a templated type as a parameter, but it has to be fully specialized.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!