Problem with boost random library.
Hi all, I'm trying to use the randome number generators but I am having some problems. I'm using boost 1.31.0 and gcc 3.2 on Windows 2000. Here is my code : printf(" Die \n "); //std::cout << "Die Rolls" << std::endl; try { boost::mt19937 rng; // produces randomness out of thin air // see pseudo-random number generators boost::uniform_int<> six(1,6); // distribution that maps to 1..6 // see random number distributions boost::variate_generator<boost::mt19937, boost::uniform_int<>
die(rng, six); // glues randomness with mapping int x = die(); printf(" Die %d\n ",x);
} catch(...) { std::cout << "exception" << std::endl; printf(" Die exception \n "); } I basically copied the example code off the web site to see if I could get it to work but I'm not having any luck. If I have the variate_generator defined as above I do not not get any output to the console after the declaration of die. I tried printf's and stream output but neither works, I never see the "Die %d" printed out and I do not see either of the outputs in the catch(...) section. printed either. I even had a throw in the try block at one time to see if it could get to the catch section and did not see output. When I remove the die declaration (and comment out the code that uses it) but leave the generator and the distribution then I can see output again and if I throw after the distribution declaration then I see both output statements in the catch block. So it seems like if I have the variate generator declared then I can't see any output after that, but when I comment it out I can see output again. I'm not sure what I'm doing wrong since I am basically copying a pretty simple example from the book I just can't get it to work. Ah and just incase this makes a difference I'm running this code as a test case in a boost test suite. I wanted to try to get it to work there before putting it into my main app. Thanks, Joshua.
Joshua, The following works fine for me on Windows 2000 with GCC. It's just your code turned into a complete program. That suggests the problem is with your test case, not your use of the RNG. Note that this version will always produce the same output, since it uses a default seed. If you want to produce different random sequences, you'll need to seed the generator accordingly. Kent #include <iostream> #include <boost/random.hpp> int main(int ac, char** av) { printf(" Die \n "); try { boost::mt19937 rng;thin air boost::uniform_int<> six(1,6); boost::variate_generator<boost::mt19937, boost::uniform_int<> > die(rng, six); int x = die(); printf(" Die %d\n ",x); } catch(...) { std::cout << "exception" << std::endl; printf(" Die exception \n "); } } -- Kent E. Holsinger kent@darwin.eeb.uconn.edu http://darwin.eeb.uconn.edu -- Department of Ecology & Evolutionary Biology -- University of Connecticut, U-3043 -- Storrs, CT 06269-3043
Kent Holsinger wrote:
Joshua,
The following works fine for me on Windows 2000 with GCC. It's just your code turned into a complete program. That suggests the problem is with your test case, not your use of the RNG.
Note that this version will always produce the same output, since it uses a default seed. If you want to produce different random sequences, you'll need to seed the generator accordingly.
Kent
#include <iostream> #include <boost/random.hpp>
int main(int ac, char** av) { printf(" Die \n "); try { boost::mt19937 rng;thin air boost::uniform_int<> six(1,6); boost::variate_generator<boost::mt19937, boost::uniform_int<> > die(rng, six); int x = die(); printf(" Die %d\n ",x); } catch(...) { std::cout << "exception" << std::endl; printf(" Die exception \n "); } }
You were correct. I put that code into its own program and it ran just fine.. so I'm not sure what the deal is in my test cases, I don't know why it would kill the output since its basically the exact same code. This is the strangest thing I've seen in a while and I really have no idea what might be the problem. Joshua.
Joshua Little wrote:
Kent Holsinger wrote:
Joshua,
The following works fine for me on Windows 2000 with GCC. It's just your code turned into a complete program. That suggests the problem is with your test case, not your use of the RNG.
Note that this version will always produce the same output, since it uses a default seed. If you want to produce different random sequences, you'll need to seed the generator accordingly.
Kent
#include <iostream> #include <boost/random.hpp>
int main(int ac, char** av) { printf(" Die \n "); try { boost::mt19937 rng;thin air boost::uniform_int<> six(1,6); boost::variate_generator<boost::mt19937, boost::uniform_int<> > die(rng, six); int x = die(); printf(" Die %d\n ",x); } catch(...) { std::cout << "exception" << std::endl; printf(" Die exception \n "); } }
You were correct. I put that code into its own program and it ran just fine.. so I'm not sure what the deal is in my test cases, I don't know why it would kill the output since its basically the exact same code. This is the strangest thing I've seen in a while and I really have no idea what might be the problem.
Joshua.
I hate to respond to my own mail but my brain woke up and I ran the test through gdb and low and behold, in the test code its causing a segfault. Program received signal SIGSEGV, Segmentation fault. 0x0046208f in boost::variate_generator<boost::random::mersenne_twister<unsigned, 32, 624, 397, 31, 2567483615, 11, 7, 2636928640, 15, 4022730752, 18, 3346425566>, boost::uniform_int<int>
::variate_generator(boost::random::mersenne_twister<unsigned, 32, 624, 397, 31, 2567483615, 11, 7, 2636928640, 15, 4022730752, 18, 3346425566>, boost::uniform_int<int>) (this=0x22d150, e= {static word_size = <optimized out>, static state_size = <optimized out>, static shift_size = <optimized out>, static mask_bits = <optimized out>, static parameter_a = <optimized out>, static output_u = <optimized out>, static output_s = <optimized out>, static output_b = <optimized out>, static output_t = <optimized out>, static output_c = <optimized out>, static output_l = <optimized out>, static has_fixed_range = <optimized out>, x = {300933633, 1838352333, 1039116329, 1822211541, 2902250641, 835884317, 743498041, 2037202853, 140269601, 3129818989, 3468776265, 3042141813, 3597794481, 2144163517, 462622297, 2597716549, 3720499777, 3305777933, 2019631721, 1995498261, 1568860369, 1886915677, 891264889, 3303332069, 589975649, 2673363629, 1613469065, 3473388469, 3774879985, 1195980285, 56300697, 1687438213, 1437389441, 882253389, 3658296489, 1954175061, 3570011409, 3045544861, 2419715513, 1563345445, 3228719265, 1118971373, 2592237513, 3246084341, 2211530033, 1950934333, 3074468569, 2991510725, 2662556353, 2490032525, 681035497, 4253883797, 1377190225, 510946013, 3078867961, 1910438757, 2109239521, 2068762925, 2414463497, 3884072501, 1051296113, 1254122621, 94884121, 3726226949, 3738829569, 2310829261, 1386541353, 2138911445, 2779481489, 3853734429, 1575041593, 3616113829, 427858209, 2363640941, 2609232969, 284195701, 1112329649, 3416503229, 368346969, 2265507653, 2099557185, 3334395917, 2950212457, 2090768405, 1892538833, 2729970013, 2939566201, 1203920357, 3008287073, 1746966445, 2709143177, 3264874677, 2961123825, 4108767997, 2827668889, 3509610629, 1637315457, 1252395853, 1087829417, 3427093845, 1707163153, 2314637469, 2422654649, 2703066917, 195500449, 3938300653, 1324044489, 1985144309, 3691288113, 25018941, 1456382937, 2779003333, 892748737, 2712014477, 4014198761, 3670364821, 2278142545, 2900551645, 3747013881, 657391717, 3289215457, 527277613, 1509749513, 3673101109, 2292257393, 2841411965, 3542354457, 4067973893, 2718245889, 619897293, 3452123689, 3916605397, 1797994129, 972099357, 3001717561, 3263875493, 2667960865, 2112117101, 3251840329, 339906677, 723033777, 1635193021, 686751833, 3938503749, 2466780225, 985695501, 1475949673, 1319193877, 1965673169, 3163883101, 2600849785, 1086644965, 3222557281, 718660781, 244443017, 4213267893, 690961137, 2613145597, 4237526681, 1183944069, 1950553217, 2520971341, 2995371689, 3047506517, 470380305, 1564659101, 3767312313, 2490453029, 3955022497, 938885101, 2438805961, 1566538485, 430499633, 120561469, 3413482713, 2197724869, 1924801729, 2007907213, 3944274153, 1590855573, 655237969, 560882909, 3311599097, 354682213, 3347280609, 3824773933, 2757303305, 1037101109, 71934833, 3484786301, 737755929, 572260357, 3161539841, 3962982093, 944407337, 1602034901, 4001097617, 730582045, 3295472697, 3211857573...}, i = 624}, d={_min = 1, _max = 6, _range = 5}) at e:/boost_1_31_0/boost/random/variate_generator.hpp:103 103 : _eng(decorated_engine(e)), _dist(d) { } (gdb)
I'm hoping someone mught recognize this error and know what I can do to fix it. This works fine in a stand alone app but it segfaults when its used in my actual program. I do have an overloaded new operator but I didn't see new mentioned in the stacktrace so I don't think thats whats doing it but I may be wrong. I see the 'optomized out' stuff that I guess might be causing the problem but thats also just a wild guess. Anyone have any ideas? Thanks, Joshua.
Joshua Little wrote:
Joshua Little wrote:
Kent Holsinger wrote:
Joshua,
The following works fine for me on Windows 2000 with GCC. It's just your code turned into a complete program. That suggests the problem is with your test case, not your use of the RNG.
Note that this version will always produce the same output, since it uses a default seed. If you want to produce different random sequences, you'll need to seed the generator accordingly.
Kent
#include <iostream> #include <boost/random.hpp>
int main(int ac, char** av) { printf(" Die \n "); try { boost::mt19937 rng;thin air boost::uniform_int<> six(1,6); boost::variate_generator<boost::mt19937, boost::uniform_int<> > die(rng, six); int x = die(); printf(" Die %d\n ",x); } catch(...) { std::cout << "exception" << std::endl; printf(" Die exception \n "); } }
You were correct. I put that code into its own program and it ran just fine.. so I'm not sure what the deal is in my test cases, I don't know why it would kill the output since its basically the exact same code. This is the strangest thing I've seen in a while and I really have no idea what might be the problem.
Joshua.
I hate to respond to my own mail but my brain woke up and I ran the test through gdb and low and behold, in the test code its causing a segfault. Program received signal SIGSEGV, Segmentation fault. 0x0046208f in boost::variate_generator<boost::random::mersenne_twister<unsigned, 32, 624, 397, 31, 2567483615, 11, 7, 2636928640, 15, 4022730752, 18, 3346425566>, boost::uniform_int<int>
::variate_generator(boost::random::mersenne_twister<unsigned, 32, 624, 397, 31, 2567483615, 11, 7, 2636928640, 15, 4022730752, 18, 3346425566>, boost::uniform_int<int>) (this=0x22d150, e= {static word_size = <optimized out>, static state_size = <optimized out>, static shift_size = <optimized out>, static mask_bits = <optimized out>, static parameter_a = <optimized out>, static output_u = <optimized out>, static output_s = <optimized out>, static output_b = <optimized out>, static output_t = <optimized out>, static output_c = <optimized out>, static output_l = <optimized out>, static has_fixed_range = <optimized out>, x = {300933633, 1838352333, 1039116329, 1822211541, 2902250641, 835884317, 743498041, 2037202853, 140269601, 3129818989, 3468776265, 3042141813, 3597794481, 2144163517, 462622297, 2597716549, 3720499777, 3305777933, 2019631721, 1995498261, 1568860369, 1886915677, 891264889, 3303332069, 589975649, 2673363629, 1613469065, 3473388469, 3774879985, 1195980285, 56300697, 1687438213, 1437389441, 882253389, 3658296489, 1954175061, 3570011409, 3045544861, 2419715513, 1563345445, 3228719265, 1118971373, 2592237513, 3246084341, 2211530033, 1950934333, 3074468569, 2991510725, 2662556353, 2490032525, 681035497, 4253883797, 1377190225, 510946013, 3078867961, 1910438757, 2109239521, 2068762925, 2414463497, 3884072501, 1051296113, 1254122621, 94884121, 3726226949, 3738829569, 2310829261, 1386541353, 2138911445, 2779481489, 3853734429, 1575041593, 3616113829, 427858209, 2363640941, 2609232969, 284195701, 1112329649, 3416503229, 368346969, 2265507653, 2099557185, 3334395917, 2950212457, 2090768405, 1892538833, 2729970013, 2939566201, 1203920357, 3008287073, 1746966445, 2709143177, 3264874677, 2961123825, 4108767997, 2827668889, 3509610629, 1637315457, 1252395853, 1087829417, 3427093845, 1707163153, 2314637469, 2422654649, 2703066917, 195500449, 3938300653, 1324044489, 1985144309, 3691288113, 25018941, 1456382937, 2779003333, 892748737, 2712014477, 4014198761, 3670364821, 2278142545, 2900551645, 3747013881, 657391717, 3289215457, 527277613, 1509749513, 3673101109, 2292257393, 2841411965, 3542354457, 4067973893, 2718245889, 619897293, 3452123689, 3916605397, 1797994129, 972099357, 3001717561, 3263875493, 2667960865, 2112117101, 3251840329, 339906677, 723033777, 1635193021, 686751833, 3938503749, 2466780225, 985695501, 1475949673, 1319193877, 1965673169, 3163883101, 2600849785, 1086644965, 3222557281, 718660781, 244443017, 4213267893, 690961137, 2613145597, 4237526681, 1183944069, 1950553217, 2520971341, 2995371689, 3047506517, 470380305, 1564659101, 3767312313, 2490453029, 3955022497, 938885101, 2438805961, 1566538485, 430499633, 120561469, 3413482713, 2197724869, 1924801729, 2007907213, 3944274153, 1590855573, 655237969, 560882909, 3311599097, 354682213, 3347280609, 3824773933, 2757303305, 1037101109, 71934833, 3484786301, 737755929, 572260357, 3161539841, 3962982093, 944407337, 1602034901, 4001097617, 730582045, 3295472697, 3211857573...}, i = 624}, d={_min = 1, _max = 6, _range = 5}) at e:/boost_1_31_0/boost/random/variate_generator.hpp:103 103 : _eng(decorated_engine(e)), _dist(d) { } (gdb)
I was wondering if anyone had any advice on how to track down this problem. I'm not sure why it works fine in a stand alone app but within my game it causes the segfault. I don't see anything that really looks suspicious, the part of the app that I was using is just straight c++, its not in the graphics code or other game logic and the segfault seems to occur in the constructor. I'm linking into a graphics/game engine but even if I am just testing the ship library by itself in a test case it had the segfault. I can't think of what might be the problem, I'm not doing anything wierd as far as I know and I'm honestly not sure how the constructor could segfault since I have plenty of memory. I've only used 1.31.0 so I'm not sure if the nightly CVS might work better but since I can use the variate generator in a stand alone app I think I must just be using it wrong. Any help would be appreciated. Joshua
Just to make sure I answer my own mails as much as possible. I tried a few different random generators. Rand48, mt11213b, and minstd_rand and they all worked inside of my game. So it seems like mt19937 and the lagged_fibonacci's are causing the problem. So obviously I can use one of the other generatrors but its still bothers me that the mt19937 and lagged_fibonaccis generators segfaults for reasons that are not apparent to me. Maybe this information will help give ideas to anyone who might know what might be wrong, maybe there is a difference between the generator types that might be causing the problem? Thanks, Joshua.
participants (2)
-
Joshua Little
-
Kent Holsinger