RandomTests.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. module Tests;
  2. import Core.Random;
  3. import Core.Test;
  4. import Core.Array;
  5. using Core::Random;
  6. static void testAverage(bool light) {
  7. int limit = light ? 100'000 : 1'000'000;
  8. Random r(553);
  9. float sum = 0;
  10. for(int i = 0; i < limit; i++) {
  11. sum += static_cast<float>(r.nextU32(2, 11));
  12. }
  13. sum /= static_cast<float>(limit);
  14. Core::testFloat(6.0f, sum, 0.01f);
  15. }
  16. static void testCoin(bool light) {
  17. int limit = light ? 100'000 : 1'000'000;
  18. Random r(5533);
  19. int c[2] = {0, 0};
  20. for(int i = 0; i < limit; i++) {
  21. c[r.nextBool()]++;
  22. }
  23. Core::testFloat(
  24. 0.0f, static_cast<float>(c[0] - c[1]) / static_cast<float>(limit),
  25. 0.003f);
  26. }
  27. static void testDistribution(bool light) {
  28. size_t limit = light ? 100'000 : 1'000'000;
  29. Random r(553);
  30. Core::Array<int, 102> c;
  31. for(size_t i = 0; i < limit; i++) {
  32. c[r.nextSize(1, c.getLength() - 1)]++;
  33. }
  34. Core::test(0, c[0]);
  35. Core::test(0, c[c.getLength() - 1]);
  36. for(size_t i = 1; i < c.getLength() - 1; i++) {
  37. Core::testFloat(
  38. 0.01f, static_cast<float>(c[i]) / static_cast<float>(limit),
  39. 0.001f);
  40. }
  41. }
  42. static void testFloatAverage(bool light) {
  43. int limit = light ? 100'000 : 1'000'000;
  44. Core::Random r(553);
  45. float sum = 0;
  46. for(int i = 0; i < limit; i++) {
  47. sum += r.nextFloat();
  48. }
  49. sum /= static_cast<float>(limit);
  50. Core::testFloat(0.5f, sum, 0.001f);
  51. }
  52. static void testFloatCoin(bool light) {
  53. int limit = light ? 100'000 : 1'000'000;
  54. Random r(5535);
  55. Core::Array<int, 2> c;
  56. for(int i = 0; i < limit; i++) {
  57. c[r.nextFloat() < 0.5f]++;
  58. }
  59. Core::testFloat(
  60. 0.0f, static_cast<float>(c[0] - c[1]) / static_cast<float>(limit),
  61. 0.003f);
  62. }
  63. static void testFloatDistribution(bool light) {
  64. int limit = light ? 100'000 : 1'000'000;
  65. Random r(553);
  66. Core::Array<int, 102> c;
  67. for(int i = 0; i < limit; i++) {
  68. c[static_cast<size_t>(1.0f + r.nextFloat() * (c.getLength() - 2))]++;
  69. }
  70. Core::test(0, c[0]);
  71. Core::test(0, c[c.getLength() - 1]);
  72. for(size_t i = 1; i < c.getLength() - 1; i++) {
  73. Core::testFloat(
  74. 0.01f, static_cast<float>(c[i]) / static_cast<float>(limit),
  75. 0.003f);
  76. }
  77. }
  78. static void testRandomI32() {
  79. Random r(56'346);
  80. Core::Array<int, 7> c;
  81. c.fill(0);
  82. for(int i = 0; i < 10'000; i++) {
  83. i32 index = r.nextI32(-2, 3) + 3;
  84. if(Core::testTrue(index >= 0)) {
  85. c[static_cast<size_t>(index)]++;
  86. }
  87. }
  88. for(size_t i = 0; i < c.getLength(); i++) {
  89. Core::test(i != 0 && i != c.getLength() - 1, c[i] > 0);
  90. }
  91. }
  92. void testRandom(bool light) {
  93. testAverage(light);
  94. testCoin(light);
  95. testDistribution(light);
  96. testFloatAverage(light);
  97. testFloatCoin(light);
  98. testFloatDistribution(light);
  99. testRandomI32();
  100. }