fork(1) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <functional>
  4. #include <random>
  5. #include <time.h>
  6.  
  7. int main() {
  8. std::mt19937 mersenne_engine(time(NULL));
  9. std::uniform_int_distribution<unsigned int> distribution(1, 10);
  10. auto generator = std::bind(distribution, mersenne_engine);
  11.  
  12. for(unsigned int i=0; i<10; ++i) {
  13. std::vector<unsigned int> weights(10);
  14.  
  15. // This line always generates the same vector content.
  16. std::generate(weights.begin(), weights.end(), generator);
  17. // This line properly randomizes the content each iteration.
  18. //for(auto &weight : weights) weight = generator();
  19.  
  20. for(auto &weight : weights) {
  21. std::cout << weight << " ";
  22. }
  23. std::cout << std::endl;
  24. }
  25. }
  26.  
Success #stdin #stdout 0s 4452KB
stdin
Standard input is empty
stdout
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7 
2 5 2 3 4 5 4 7 8 7