How to generate a random number in C++?

后端 未结 11 1528
萌比男神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:55

    #include 
    #include 
    #include 
    
    int main() {
        srand(time(NULL));
        int random_number = std::rand(); // rand() return a number between ​0​ and RAND_MAX
        std::cout << random_number;
        return 0;
    }
    

    http://en.cppreference.com/w/cpp/numeric/random/rand

提交回复
热议问题