C++ generating random numbers

后端 未结 11 1623
我寻月下人不归
我寻月下人不归 2020-12-01 10:49

My output is 20 random 1\'s, not between 10 and 1, can anyone explain why this is happening?

#include  
#include  
#include <         


        
11条回答
  •  误落风尘
    2020-12-01 11:34

    It's much easier to use the library correctly than rand (assuming you're familiar enough with C++ that the syntax doesn't throw you).

    #include 
    #include 
    
    int main() {
      std::random_device r;
      std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
      std::mt19937 eng(seed);
    
      std::uniform_int_distribution<> dist(1, 10);
    
      for(int i = 0; i < 20; ++i)
        std::cout << dist(eng) << " ";
    }
    

提交回复
热议问题