You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/random.h"
  11. #include <math.h>
  12. #include <limits>
  13. #include <vector>
  14. #include "rtc_base/numerics/math_utils.h" // unsigned difference
  15. #include "test/gtest.h"
  16. namespace webrtc {
  17. namespace {
  18. // Computes the positive remainder of x/n.
  19. template <typename T>
  20. T fdiv_remainder(T x, T n) {
  21. RTC_CHECK_GE(n, 0);
  22. T remainder = x % n;
  23. if (remainder < 0)
  24. remainder += n;
  25. return remainder;
  26. }
  27. } // namespace
  28. // Sample a number of random integers of type T. Divide them into buckets
  29. // based on the remainder when dividing by bucket_count and check that each
  30. // bucket gets roughly the expected number of elements.
  31. template <typename T>
  32. void UniformBucketTest(T bucket_count, int samples, Random* prng) {
  33. std::vector<int> buckets(bucket_count, 0);
  34. uint64_t total_values = 1ull << (std::numeric_limits<T>::digits +
  35. std::numeric_limits<T>::is_signed);
  36. T upper_limit =
  37. std::numeric_limits<T>::max() -
  38. static_cast<T>(total_values % static_cast<uint64_t>(bucket_count));
  39. ASSERT_GT(upper_limit, std::numeric_limits<T>::max() / 2);
  40. for (int i = 0; i < samples; i++) {
  41. T sample;
  42. do {
  43. // We exclude a few numbers from the range so that it is divisible by
  44. // the number of buckets. If we are unlucky and hit one of the excluded
  45. // numbers we just resample. Note that if the number of buckets is a
  46. // power of 2, then we don't have to exclude anything.
  47. sample = prng->Rand<T>();
  48. } while (sample > upper_limit);
  49. buckets[fdiv_remainder(sample, bucket_count)]++;
  50. }
  51. for (T i = 0; i < bucket_count; i++) {
  52. // Expect the result to be within 3 standard deviations of the mean.
  53. EXPECT_NEAR(buckets[i], samples / bucket_count,
  54. 3 * sqrt(samples / bucket_count));
  55. }
  56. }
  57. TEST(RandomNumberGeneratorTest, BucketTestSignedChar) {
  58. Random prng(7297352569824ull);
  59. UniformBucketTest<signed char>(64, 640000, &prng);
  60. UniformBucketTest<signed char>(11, 440000, &prng);
  61. UniformBucketTest<signed char>(3, 270000, &prng);
  62. }
  63. TEST(RandomNumberGeneratorTest, BucketTestUnsignedChar) {
  64. Random prng(7297352569824ull);
  65. UniformBucketTest<unsigned char>(64, 640000, &prng);
  66. UniformBucketTest<unsigned char>(11, 440000, &prng);
  67. UniformBucketTest<unsigned char>(3, 270000, &prng);
  68. }
  69. TEST(RandomNumberGeneratorTest, BucketTestSignedShort) {
  70. Random prng(7297352569824ull);
  71. UniformBucketTest<int16_t>(64, 640000, &prng);
  72. UniformBucketTest<int16_t>(11, 440000, &prng);
  73. UniformBucketTest<int16_t>(3, 270000, &prng);
  74. }
  75. TEST(RandomNumberGeneratorTest, BucketTestUnsignedShort) {
  76. Random prng(7297352569824ull);
  77. UniformBucketTest<uint16_t>(64, 640000, &prng);
  78. UniformBucketTest<uint16_t>(11, 440000, &prng);
  79. UniformBucketTest<uint16_t>(3, 270000, &prng);
  80. }
  81. TEST(RandomNumberGeneratorTest, BucketTestSignedInt) {
  82. Random prng(7297352569824ull);
  83. UniformBucketTest<signed int>(64, 640000, &prng);
  84. UniformBucketTest<signed int>(11, 440000, &prng);
  85. UniformBucketTest<signed int>(3, 270000, &prng);
  86. }
  87. TEST(RandomNumberGeneratorTest, BucketTestUnsignedInt) {
  88. Random prng(7297352569824ull);
  89. UniformBucketTest<unsigned int>(64, 640000, &prng);
  90. UniformBucketTest<unsigned int>(11, 440000, &prng);
  91. UniformBucketTest<unsigned int>(3, 270000, &prng);
  92. }
  93. // The range of the random numbers is divided into bucket_count intervals
  94. // of consecutive numbers. Check that approximately equally many numbers
  95. // from each inteval are generated.
  96. void BucketTestSignedInterval(unsigned int bucket_count,
  97. unsigned int samples,
  98. int32_t low,
  99. int32_t high,
  100. int sigma_level,
  101. Random* prng) {
  102. std::vector<unsigned int> buckets(bucket_count, 0);
  103. ASSERT_GE(high, low);
  104. ASSERT_GE(bucket_count, 2u);
  105. uint32_t interval = unsigned_difference<int32_t>(high, low) + 1;
  106. uint32_t numbers_per_bucket;
  107. if (interval == 0) {
  108. // The computation high - low + 1 should be 2^32 but overflowed
  109. // Hence, bucket_count must be a power of 2
  110. ASSERT_EQ(bucket_count & (bucket_count - 1), 0u);
  111. numbers_per_bucket = (0x80000000u / bucket_count) * 2;
  112. } else {
  113. ASSERT_EQ(interval % bucket_count, 0u);
  114. numbers_per_bucket = interval / bucket_count;
  115. }
  116. for (unsigned int i = 0; i < samples; i++) {
  117. int32_t sample = prng->Rand(low, high);
  118. EXPECT_LE(low, sample);
  119. EXPECT_GE(high, sample);
  120. buckets[unsigned_difference<int32_t>(sample, low) / numbers_per_bucket]++;
  121. }
  122. for (unsigned int i = 0; i < bucket_count; i++) {
  123. // Expect the result to be within 3 standard deviations of the mean,
  124. // or more generally, within sigma_level standard deviations of the mean.
  125. double mean = static_cast<double>(samples) / bucket_count;
  126. EXPECT_NEAR(buckets[i], mean, sigma_level * sqrt(mean));
  127. }
  128. }
  129. // The range of the random numbers is divided into bucket_count intervals
  130. // of consecutive numbers. Check that approximately equally many numbers
  131. // from each inteval are generated.
  132. void BucketTestUnsignedInterval(unsigned int bucket_count,
  133. unsigned int samples,
  134. uint32_t low,
  135. uint32_t high,
  136. int sigma_level,
  137. Random* prng) {
  138. std::vector<unsigned int> buckets(bucket_count, 0);
  139. ASSERT_GE(high, low);
  140. ASSERT_GE(bucket_count, 2u);
  141. uint32_t interval = high - low + 1;
  142. uint32_t numbers_per_bucket;
  143. if (interval == 0) {
  144. // The computation high - low + 1 should be 2^32 but overflowed
  145. // Hence, bucket_count must be a power of 2
  146. ASSERT_EQ(bucket_count & (bucket_count - 1), 0u);
  147. numbers_per_bucket = (0x80000000u / bucket_count) * 2;
  148. } else {
  149. ASSERT_EQ(interval % bucket_count, 0u);
  150. numbers_per_bucket = interval / bucket_count;
  151. }
  152. for (unsigned int i = 0; i < samples; i++) {
  153. uint32_t sample = prng->Rand(low, high);
  154. EXPECT_LE(low, sample);
  155. EXPECT_GE(high, sample);
  156. buckets[(sample - low) / numbers_per_bucket]++;
  157. }
  158. for (unsigned int i = 0; i < bucket_count; i++) {
  159. // Expect the result to be within 3 standard deviations of the mean,
  160. // or more generally, within sigma_level standard deviations of the mean.
  161. double mean = static_cast<double>(samples) / bucket_count;
  162. EXPECT_NEAR(buckets[i], mean, sigma_level * sqrt(mean));
  163. }
  164. }
  165. TEST(RandomNumberGeneratorTest, UniformUnsignedInterval) {
  166. Random prng(299792458ull);
  167. BucketTestUnsignedInterval(2, 100000, 0, 1, 3, &prng);
  168. BucketTestUnsignedInterval(7, 100000, 1, 14, 3, &prng);
  169. BucketTestUnsignedInterval(11, 100000, 1000, 1010, 3, &prng);
  170. BucketTestUnsignedInterval(100, 100000, 0, 99, 3, &prng);
  171. BucketTestUnsignedInterval(2, 100000, 0, 4294967295, 3, &prng);
  172. BucketTestUnsignedInterval(17, 100000, 455, 2147484110, 3, &prng);
  173. // 99.7% of all samples will be within 3 standard deviations of the mean,
  174. // but since we test 1000 buckets we allow an interval of 4 sigma.
  175. BucketTestUnsignedInterval(1000, 1000000, 0, 2147483999, 4, &prng);
  176. }
  177. TEST(RandomNumberGeneratorTest, UniformSignedInterval) {
  178. Random prng(66260695729ull);
  179. BucketTestSignedInterval(2, 100000, 0, 1, 3, &prng);
  180. BucketTestSignedInterval(7, 100000, -2, 4, 3, &prng);
  181. BucketTestSignedInterval(11, 100000, 1000, 1010, 3, &prng);
  182. BucketTestSignedInterval(100, 100000, 0, 99, 3, &prng);
  183. BucketTestSignedInterval(2, 100000, std::numeric_limits<int32_t>::min(),
  184. std::numeric_limits<int32_t>::max(), 3, &prng);
  185. BucketTestSignedInterval(17, 100000, -1073741826, 1073741829, 3, &prng);
  186. // 99.7% of all samples will be within 3 standard deviations of the mean,
  187. // but since we test 1000 buckets we allow an interval of 4 sigma.
  188. BucketTestSignedInterval(1000, 1000000, -352, 2147483647, 4, &prng);
  189. }
  190. // The range of the random numbers is divided into bucket_count intervals
  191. // of consecutive numbers. Check that approximately equally many numbers
  192. // from each inteval are generated.
  193. void BucketTestFloat(unsigned int bucket_count,
  194. unsigned int samples,
  195. int sigma_level,
  196. Random* prng) {
  197. ASSERT_GE(bucket_count, 2u);
  198. std::vector<unsigned int> buckets(bucket_count, 0);
  199. for (unsigned int i = 0; i < samples; i++) {
  200. uint32_t sample = bucket_count * prng->Rand<float>();
  201. EXPECT_LE(0u, sample);
  202. EXPECT_GE(bucket_count - 1, sample);
  203. buckets[sample]++;
  204. }
  205. for (unsigned int i = 0; i < bucket_count; i++) {
  206. // Expect the result to be within 3 standard deviations of the mean,
  207. // or more generally, within sigma_level standard deviations of the mean.
  208. double mean = static_cast<double>(samples) / bucket_count;
  209. EXPECT_NEAR(buckets[i], mean, sigma_level * sqrt(mean));
  210. }
  211. }
  212. TEST(RandomNumberGeneratorTest, UniformFloatInterval) {
  213. Random prng(1380648813ull);
  214. BucketTestFloat(100, 100000, 3, &prng);
  215. // 99.7% of all samples will be within 3 standard deviations of the mean,
  216. // but since we test 1000 buckets we allow an interval of 4 sigma.
  217. // BucketTestSignedInterval(1000, 1000000, -352, 2147483647, 4, &prng);
  218. }
  219. TEST(RandomNumberGeneratorTest, SignedHasSameBitPattern) {
  220. Random prng_signed(66738480ull), prng_unsigned(66738480ull);
  221. for (int i = 0; i < 1000; i++) {
  222. signed int s = prng_signed.Rand<signed int>();
  223. unsigned int u = prng_unsigned.Rand<unsigned int>();
  224. EXPECT_EQ(u, static_cast<unsigned int>(s));
  225. }
  226. for (int i = 0; i < 1000; i++) {
  227. int16_t s = prng_signed.Rand<int16_t>();
  228. uint16_t u = prng_unsigned.Rand<uint16_t>();
  229. EXPECT_EQ(u, static_cast<uint16_t>(s));
  230. }
  231. for (int i = 0; i < 1000; i++) {
  232. signed char s = prng_signed.Rand<signed char>();
  233. unsigned char u = prng_unsigned.Rand<unsigned char>();
  234. EXPECT_EQ(u, static_cast<unsigned char>(s));
  235. }
  236. }
  237. TEST(RandomNumberGeneratorTest, Gaussian) {
  238. const int kN = 100000;
  239. const int kBuckets = 100;
  240. const double kMean = 49;
  241. const double kStddev = 10;
  242. Random prng(1256637061);
  243. std::vector<unsigned int> buckets(kBuckets, 0);
  244. for (int i = 0; i < kN; i++) {
  245. int index = prng.Gaussian(kMean, kStddev) + 0.5;
  246. if (index >= 0 && index < kBuckets) {
  247. buckets[index]++;
  248. }
  249. }
  250. const double kPi = 3.14159265358979323846;
  251. const double kScale = 1 / (kStddev * sqrt(2.0 * kPi));
  252. const double kDiv = -2.0 * kStddev * kStddev;
  253. for (int n = 0; n < kBuckets; ++n) {
  254. // Use Simpsons rule to estimate the probability that a random gaussian
  255. // sample is in the interval [n-0.5, n+0.5].
  256. double f_left = kScale * exp((n - kMean - 0.5) * (n - kMean - 0.5) / kDiv);
  257. double f_mid = kScale * exp((n - kMean) * (n - kMean) / kDiv);
  258. double f_right = kScale * exp((n - kMean + 0.5) * (n - kMean + 0.5) / kDiv);
  259. double normal_dist = (f_left + 4 * f_mid + f_right) / 6;
  260. // Expect the number of samples to be within 3 standard deviations
  261. // (rounded up) of the expected number of samples in the bucket.
  262. EXPECT_NEAR(buckets[n], kN * normal_dist, 3 * sqrt(kN * normal_dist) + 1);
  263. }
  264. }
  265. } // namespace webrtc