Boost Random Number Generators

Started by
12 comments, last by hereticprophecy 17 years, 1 month ago
I found that Boost has a non-deterministic RNG class, but it's syntax and utilization are eluding me. I'm attempting to write a wrapper class that has functions that return the dice rolls for an n-sided die, but I keep getting unresolved externals when I attempt to create or use a boost::random_device.

//Class Definition
class Dice_Bag {
   boost::random_device RNG;
   
public:
   int Roll_d3();
   int Roll_d4();
   int Roll_d6();
   //etc.
private:
   int Roll_die(int);
};


int Dice_Bag::Roll_die(int sides) {
   return (RNG()%sides + 1);
}

...is my current attempt. I receive the following error when attempting to link:
Quote: Dice_Bag.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall boost::random_device::operator()(void)" (??Rrandom_device@boost@@QAEIXZ) referenced in function "private: int __thiscall Dice_Bag::Roll_Die(int)" (?Roll_Die@Dice_Bag@@AAEHH@Z)
Where is my syntax going wrong? Other of the boost number generators are placed inside a distributor (like mt19937) and I've gotten that to work wonderfully; I don't want deterministic behavior if I can help it. Does boost::random_device need a distributor as well? Any guidance would be greatly appreciated. Thank you!
Advertisement
Shouldn't this be :
private:   int Roll_die(int sides);
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
When declaring a member function, you don't need to name your variable...just it's type.
Your syntax isn't wrong. There's just no implementation of the () operator for the class. Most of the functionality of boost is included in the header files, but not always. In this case you'll see that some of the methods aren't implemented. This means you need to probably link to the library file for your particular compiler. This makes sense: the non-deterministic number generator is entirely dependent on the environment. One may not even exist on some computers. Link to the library. If you don't have the library, create one. If you don't know how, use this.
EDIT:
Yup, here's the file if you're curious.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

The non-deterministic RNG is not available on all platforms. In the boost libraries, it seems to only be implemented for linux using /dev/urandom by default.

If you want it to work on other platforms, you will have to implement random_device yourself.


jfl
Original post by hereticprophecy
int Dice_Bag::Roll_die(int sides) {   return (RNG()%sides + 1);}


I'm told using % like that for generating random values is a bad idea because it "favors the low bits". I'm not too sure how that works exactly, but the people who brought this to much attention are much smarter than me.

EDIT: Hmm. Seems you're not allowed to quote source boxes
It favors the low numbers. Imagine if you RNG() returned a number 0..15, then you used RNG()%10, then the numbers 0..5 would turn up twice as much as the numbers 6..9

In practice it's not much of a concern since RNG() returns a larger range, but if it is, then the standard way of fixing it is to use % with a number that is a factor of the upper limit of RND() and bigger that the range you want (usually picking a power of 2) and use that to repick random numbers until you get one that falls in the range you want.
Quote:Original post by nobodynews
Your syntax isn't wrong. There's just no implementation of the () operator for the class. Most of the functionality of boost is included in the header files, but not always. In this case you'll see that some of the methods aren't implemented. This means you need to probably link to the library file for your particular compiler. This makes sense: the non-deterministic number generator is entirely dependent on the environment. One may not even exist on some computers. Link to the library. If you don't have the library, create one. If you don't know how, use this.
EDIT:
Yup, here's the file if you're curious.


I used the installed prior to attempting this (I had located the .cpp file last night but just figured it was linked automatically) but I'm not sure if I'm telling VC++ to look in the proper directories. Do I point libraries, executables, and includes toward the boot_1_33_1 directory or do they point at more specific folders? Or am I missing the point of your post entirely?
Quote:Original post by hereticprophecy
I used the installed prior to attempting this (I had located the .cpp file last night but just figured it was linked automatically) but I'm not sure if I'm telling VC++ to look in the proper directories. Do I point libraries, executables, and includes toward the boot_1_33_1 directory or do they point at more specific folders? Or am I missing the point of your post entirely?


I took another look at the header file and saw that it wasn't just operator() that didn't have an implementation, it was also the constructor, destructor, and a method called entropy. If the only thing giving you a problem is operator() then it probably isn't a problem with the location of your lib files. Because if it were, then you'd have four linker errors. In which case I don't have a clue why it's giving you a problem. Sorry. With that said, I'm not sure there IS a library file for random_device. I decided to test it out and I had to add the file I linked above directly to my project. But then, maybe MY locations are wrong.

The point is moot as this random number generator only works on linux as is. You'd have to figure out how to get chaotic information from another source in windows. And I don't know anything about that. Sorry again.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Hey, at least I can stop trying to fiddle with this and start dealing with the others I already know how to work with. I appreciate your help!

This topic is closed to new replies.

Advertisement