Thread: problem with boost random func (again)

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    problem with boost random func (again)

    Hello..
    Here is the code:

    Code:
    int my_rand(int min, int max) {
    	mt19937 rng;
    	rng.seed( time_seed() );
    
    	uniform_int<> dist(min, max);
    	variate_generator<mt19937&, uniform_int<> > die(rng, dist);
    
    	return die();
    }

    I know I have to seed only once, but when I put boost::mt19937 outside the function in a header, I get linker errors (no idea why)..

    The errors:
    main.obj : error LNK2005: "class boost::random::mersenne_twister<unsigned long,32,624,397,31,2567483615,11,7,2636928640,15,4 022730752,18,3346425566> rng" (?rng@@3V?$mersenne_twister@K$0CA@$0CHA@$0BIN@$0BP @$0JJAILANP@$0L@$06$0JNCMFGIA@$0P@$0OPMGAAAA@$0BC@ $0MHHGGGNO@@random@boost@@A) already defined in http.obj

    What could be wrong?
    Thanks for help

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Two things about the error reported:

    main.obj : error LNK2005: "class boost::random::mersenne_twister<unsigned long,32,624,397,31,2567483615,11,7,2636928640,15,4 022730752,18,3346425566> rng" (?rng@@3V?$mersenne_twister@K$0CA@$0CHA@$0BIN@$0BP @$0JJAILANP@$0L@$06$0JNCMFGIA@$0P@$0OPMGAAAA@$0BC@ $0MHHGGGNO@@random@boost@@A) already defined in http.obj

    The first tells you that rng is the problem. mt19937 is in fact a typedef for that long an awkward template class you see there. By this time the linker already has no knowledge of typedefs name substitution. That is simply an aid for us humans.

    The second tells you that there is a redefinition of rng. rng is being defined twice. And this is the actual error. At this point you should know the difference between declaring and defining a name.

    int val;

    Defines an integer named val. In other words, it creates the variable and allocates storage for it.

    extern int val;

    Declares an integer named val. The definition will happen somewhere else. In other words it simply introduces this name into the scope delegation its definition to some cpp file that includes the header where this extern is.

    You should never define variables in header files for the reason you just found out. Changes are you will come across multiple definitions of a name. And the rules are multiples declarations are ok, as long as there is just one definition

    You provide little information as to why you want to move that definition to a header file. But no matter what, the solution is to prepend extern
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks.. You helped A LOT!

    But one more thing.. Where would be the right way to define a variable, if I have to call it from main function, and I have functions in other files that work with it? Should I define it in main, and declare it in header file of function that work with/use that variable?

    Thank you again!

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    On the particular case of a mersenne twister implementation, I don't see a problem with creating as many instances as needed when you want to deal with random numbers. Two different functions using it should each declare their own MT generator at function scope.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    I just want to make seed from the main function and then call mt19937 variable with random function..

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    rnd.cpp:
    Code:
    namespace rnd {
    namespace {
    mt19937 rng;
    }
    
    void my_srand() {
    	detail::rng.seed( time_seed() );
    }
    
    int my_rand(int min, int max) {
    	uniform_int<> dist(min, max);
    	variate_generator<mt19937&, uniform_int<> > die(detail::rng, dist);
    
    	return die();
    }
    }
    rnd.hpp:
    Code:
    namespace rnd {
      void my_srand();
      int my_rand(int min, int max);
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 09:22 AM
  2. Dev C++ Func Problem
    By Cris987 in forum C++ Programming
    Replies: 1
    Last Post: 01-03-2004, 02:35 AM
  3. problem with random integer
    By techno logic in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2002, 02:20 PM
  4. random problem
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2002, 02:39 PM
  5. Random things are fine, just one problem with them.
    By DarkSFK in forum C++ Programming
    Replies: 14
    Last Post: 08-19-2002, 08:40 AM