How to generate a random number in C++?

后端 未结 11 1572
萌比男神i
萌比男神i 2020-11-22 11:14

I\'m trying to make a game with dice, and I need to have random numbers in it (to simulate the sides of the die. I know how to make it between 1 and 6). Using



        
11条回答
  •  忘掉有多难
    2020-11-22 11:43

    If you are using boost libs you can obtain a random generator in this way:

    #include 
    #include 
    
    // Used in randomization
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace boost;
    
    int current_time_nanoseconds(){
        struct timespec tm;
        clock_gettime(CLOCK_REALTIME, &tm);
        return tm.tv_nsec;
    }
    
    int main (int argc, char* argv[]) {
        unsigned int dice_rolls = 12;
        random::mt19937 rng(current_time_nanoseconds());
        random::uniform_int_distribution<> six(1,6);
    
        for(unsigned int i=0; i

    Where the function current_time_nanoseconds() gives the current time in nanoseconds which is used as a seed.


    Here is a more general class to get random integers and dates in a range:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include "boost/date_time/posix_time/posix_time.hpp"
    #include "boost/date_time/gregorian/gregorian.hpp"
    
    
    using namespace std;
    using namespace boost;
    using namespace boost::posix_time;
    using namespace boost::gregorian;
    
    
    class Randomizer {
    private:
        static const bool debug_mode = false;
        random::mt19937 rng_;
    
        // The private constructor so that the user can not directly instantiate
        Randomizer() {
            if(debug_mode==true){
                this->rng_ = random::mt19937();
            }else{
                this->rng_ = random::mt19937(current_time_nanoseconds());
            }
        };
    
        int current_time_nanoseconds(){
            struct timespec tm;
            clock_gettime(CLOCK_REALTIME, &tm);
            return tm.tv_nsec;
        }
    
        // C++ 03
        // ========
        // Dont forget to declare these two. You want to make sure they
        // are unacceptable otherwise you may accidentally get copies of
        // your singleton appearing.
        Randomizer(Randomizer const&);     // Don't Implement
        void operator=(Randomizer const&); // Don't implement
    
    public:
        static Randomizer& get_instance(){
            // The only instance of the class is created at the first call get_instance ()
            // and will be destroyed only when the program exits
            static Randomizer instance;
            return instance;
        }
        bool method() { return true; };
    
        int rand(unsigned int floor, unsigned int ceil){
            random::uniform_int_distribution<> rand_ = random::uniform_int_distribution<> (floor,ceil);
            return (rand_(rng_));
        }
    
        // Is not considering the millisecons
        time_duration rand_time_duration(){
            boost::posix_time::time_duration floor(0, 0, 0, 0);
            boost::posix_time::time_duration ceil(23, 59, 59, 0);
            unsigned int rand_seconds = rand(floor.total_seconds(), ceil.total_seconds());
            return seconds(rand_seconds);
        }
    
    
        date rand_date_from_epoch_to_now(){
            date now = second_clock::local_time().date();
            return rand_date_from_epoch_to_ceil(now);
        }
    
        date rand_date_from_epoch_to_ceil(date ceil_date){
            date epoch = ptime(date(1970,1,1)).date();
            return rand_date_in_interval(epoch, ceil_date);
        }
    
        date rand_date_in_interval(date floor_date, date ceil_date){
            return rand_ptime_in_interval(ptime(floor_date), ptime(ceil_date)).date();
        }
    
        ptime rand_ptime_from_epoch_to_now(){
            ptime now = second_clock::local_time();
            return rand_ptime_from_epoch_to_ceil(now);
        }
    
        ptime rand_ptime_from_epoch_to_ceil(ptime ceil_date){
            ptime epoch = ptime(date(1970,1,1));
            return rand_ptime_in_interval(epoch, ceil_date);
        }
    
        ptime rand_ptime_in_interval(ptime floor_date, ptime ceil_date){
            time_duration const diff = ceil_date - floor_date;
            long long gap_seconds = diff.total_seconds();
            long long step_seconds = Randomizer::get_instance().rand(0, gap_seconds);
            return floor_date + seconds(step_seconds);
        }
    };
    

提交回复
热议问题