fork(2) download
  1. #include <iostream>
  2. #include <random>
  3. using namespace std;
  4.  
  5. int* statGen(const int die, const int num)
  6. {
  7. int x;
  8. int* stats = new int[num];
  9. int tot = (die + 1) / 2 * num;
  10.  
  11. std::random_device rd; // only used once to initialise (seed) engine
  12. std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
  13. std::uniform_int_distribution<int> uni(0, num-1); // guaranteed unbiased
  14.  
  15. while(tot > 0)
  16. {
  17. x = uni(rng); //generate a random number between 1 and then number of stats
  18. if(stats[x] < die)
  19. {
  20. ++stats[x];
  21. --tot;
  22. }
  23. }
  24. return stats;
  25. }
  26.  
  27. int main()
  28. {
  29. int* myStats;
  30. for(int i = 0; i < 1000; i++)
  31. {
  32. myStats = statGen(20, 6);
  33. cout << '[';
  34. for(int j = 0; j < 5; j++)
  35. {
  36. cout << myStats[j] << ' ';
  37. }
  38. cout << myStats[5] << ']' << endl;
  39. delete[] myStats;
  40. }
  41. return 0;
  42. }
Time limit exceeded #stdin #stdout 5s 4272KB
stdin
Standard input is empty
stdout
[9 15 6 11 6 13]
[11 15 14 20 16 20]