RandomTests.cpp 2.9 KB

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