test_random.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_BASE_TEST_RANDOM_H_
  5. #define MEDIA_BASE_TEST_RANDOM_H_
  6. #include <stdint.h>
  7. #include "base/check_op.h"
  8. // Vastly simplified ACM random class meant to only be used for testing.
  9. // This class is meant to generate predictable sequences of pseudorandom
  10. // numbers, unlike the classes in base/rand_util.h which are meant to generate
  11. // unpredictable sequences.
  12. // See
  13. // https://code.google.com/p/szl/source/browse/trunk/src/utilities/acmrandom.h
  14. // for more information.
  15. namespace media {
  16. class TestRandom {
  17. public:
  18. explicit TestRandom(uint32_t seed) {
  19. seed_ = seed & 0x7fffffff; // make this a non-negative number
  20. if (seed_ == 0 || seed_ == M) {
  21. seed_ = 1;
  22. }
  23. }
  24. int32_t Rand() {
  25. static const uint64_t A = 16807; // bits 14, 8, 7, 5, 2, 1, 0
  26. seed_ = static_cast<int32_t>((seed_ * A) % M);
  27. CHECK_GT(seed_, 0);
  28. return seed_;
  29. }
  30. private:
  31. static const uint64_t M = 2147483647L; // 2^32-1
  32. int32_t seed_;
  33. };
  34. } // namespace media
  35. #endif // MEDIA_BASE_TEST_RANDOM_H_