Thread: problems using Random_Device

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    problems using Random_Device

    Hello,

    My question is if Random_Device works on windows . If so, what am I doing wrong in this implementation?

    Code:
    #include <boost/random/mersenne_twister.hpp>
    #include <boost/random/uniform_int_distribution.hpp>
    #include <random>
    #include<iostream>
    #include<exception>
    
    //This will create our ruler Dice
    boost::random::mt19937 gen;
    std::random_device rd;
    int Roll_d( int n)
    {
        gen.seed( rd());
        boost::random::uniform_int_distribution<> dist(1, n);
        return dist(gen);
    
    }
    I get a thrown std::runtime_error at me.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    at first glance, I'd check to be sure that you can mix boost and std objects in the way you're trying to do it here.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    Yeah, I realize that as well. I did changed it, but I still get the same error.

    Code:
    #include <random>
    #include<iostream>
    
    
    
    
    int main(void)
    {
       std::random_device rd;
       std::mt19937 gen(rd());
       std::uniform_int_distribution<> dice(1, 6);
       std::cout<< dice(gen) ;
       std::cin.get();
       return 0;
    }
    }

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    so why not just use

    Code:
    rd=rand()%6+1;
    


    ?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    @darkInuyasha: do not attempt to default-construct random_device on a windows machine. It will probably try to open dev/random. More importantly windows does not have a similar hardware source of randomness by default, so god knows what a working implementation uses. Default constructing a marsenne twister should work fine, as would seeding the prng from basically any other source. For your edification:
    Code:
    #include <random>
    #include <iostream>
    #include <stdexcept>
    
    int main( void ) {
        try {
            std::random_device rd;
            std::mt19937 gen( rd() );
            std::uniform_int_distribution<> dice( 1, 6 );
            std::cout << dice( gen ) ;
            std::cin.get();
        } catch( std::exception &err ) {
            std::cerr << err.what() << std::endl;
        }
        return 0;
    }
    
    /*
    my output:
    random_device::random_device(const std::string&)
    */
    Last edited by whiteflags; 01-19-2013 at 02:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with HW Problems!!!
    By NikeLover in forum C++ Programming
    Replies: 12
    Last Post: 02-23-2011, 05:17 AM
  2. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  3. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 01:47 AM
  4. Three problems
    By CBUK in forum C Programming
    Replies: 8
    Last Post: 01-28-2005, 11:31 AM
  5. EOF problems - plz help
    By DenisGFX in forum C Programming
    Replies: 0
    Last Post: 02-06-2002, 03:52 PM