Public Group

# Boost Random Numbers

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

## Recommended Posts

Right i'm just trying to get some simple randomness using boost, firstly i wondered if the code below is ok;
int RandInt( int upper, int lower = 0 )
{
if( upper < lower )
{
std::swap(upper, lower);
}

boost::mt19937 rng;

boost::uniform_int<> myRange(lower, upper);

boost::variate_generator<boost::mt19937&, boost::uniform_int<> > myRand(rng, myRange);

return myRand();
}


Is this overkill? I know i can use rand() for simple numbers but i'd like to use boost. I want to check that the above is reuseable, so maybe i could put it in a static class of Random-like things. Should 'rng' be a class memeber if this is the case? Should i be using the mt19937 for my generator, do others have advantages? Help much appreciated. [Edited by - JimmyDeemo on April 21, 2008 6:46:03 AM]

##### Share on other sites
1. It seems somewhat unintuitive to have the argument for the upper end of the range first...
2. std::swap()!
3. I'm guessing this hasn't been compiled (higher != upper)

I don't think using boost::random is overkill. However, you'll most likely want to create and re-use a random generator object, rather than creating it from scratch each time (I don't have the docs in front of me, but I'm guessing your current method will generate the same value on every call).

As for which generator to use, I think it's just a matter of choosing a balance of quality, speed, and memory usage that you're comfortable with (I used minstd_rand the last time I used boost::random, which worked fine).

##### Share on other sites
Quote:
 Original post by jyk1. It seems somewhat unintuitive to have the argument for the upper end of the range first...2. std::swap()!3. I'm guessing this hasn't been compiled (higher != upper)

1. This was just because i thought a lot of the time i'd be generating a number between 0 and whatever, so having the lower range second i can give it zero as default.

2. Your right i should use this.

3. No it hasn't i just jotted it down quickly.

Quote:
 Original post by jykHowever, you'll most likely want to create and re-use a random generator object

Thats what i thought, if i had a random number class i could create one object and use it for multiple things. I don't need to seed the boost stuff do i?

As for quality, speed and memory usage, i'm finding it hard to get that info from the boost docs. Or should i be looking into the algorithms themselves. Its not like i'm calculating radiation doses with them so speed and memory would concern me more.

According to this the one i'm using it probably the best of the integer rng's. Is that a fair assumption?

##### Share on other sites
Quote:
 1. This was just because i thought a lot of the time i'd be generating a number between 0 and whatever, so having the lower range second i can give it zero as default.
I would instead create an overload that takes only one argument (the upper end of the range), and then calls the other function with a 'lower' argument of zero. (Also, I'd probably assert or throw if the arguments are in the wrong order, rather than silently swapping them.)
Quote:
 Thats what i thought, if i had a random number class i could create one object and use it for multiple things. I don't need to seed the boost stuff do i?
I think you will want call seed() at some point - otherwise, I believe the same sequence of numbers will be generated every time the application is run. (Of course, you may wish to save the seed and/or the generator itself under some circumstances for the purpose of reproducibility.)
Quote:
 according to this the one i'm using it probably the best of the integer rng's. Is that a fair assumption?
I don't know :) I kind of doubt that there's a single 'best' choice - it just depends on what your priorities are.

##### Share on other sites
Quote:
 According to this the one i'm using it probably the best of the integer rng's. Is that a fair assumption?

Mersenne Twister is probably the best pseudo random number generator out there for uniform non-cryptographic randomness. You can predict the outcome of a Mersenne Twister random generator pretty easily just by seeing a short sequence of generated numbers which is why it isn't cryptographically secure. It also has a pretty heavy state so if you're in a memory constrained environment it's not ideal. Also boost afaik doesn't use the best performing Mersenne implementations out there. But unless the main purpose of your application is to generate random numbers I doubt the impact will be significant and the MSVC rand() implementation is as bad as it gets last time I looked.

• 11
• 19
• 12
• 34
• 9
• ### Forum Statistics

• Total Topics
631398
• Total Posts
2999837
×