2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
12 #ifndef TEST_ACM_RANDOM_H_
13 #define TEST_ACM_RANDOM_H_
15 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
17 #include "aom/aom_integer.h"
19 namespace libaom_test
{
23 ACMRandom() : random_(DeterministicSeed()) {}
25 explicit ACMRandom(int seed
) : random_(seed
) {}
27 void Reset(int seed
) { random_
.Reseed(seed
); }
29 uint32_t Rand31(void) {
30 return random_
.Generate(testing::internal::Random::kMaxRange
);
33 uint16_t Rand16(void) {
34 const uint32_t value
=
35 random_
.Generate(testing::internal::Random::kMaxRange
);
36 return (value
>> 15) & 0xffff;
39 int16_t Rand15Signed(void) {
40 const uint32_t value
=
41 random_
.Generate(testing::internal::Random::kMaxRange
);
42 return (value
>> 17) & 0xffff;
45 uint16_t Rand12(void) {
46 const uint32_t value
=
47 random_
.Generate(testing::internal::Random::kMaxRange
);
48 // There's a bit more entropy in the upper bits of this implementation.
49 return (value
>> 19) & 0xfff;
52 int16_t Rand9Signed(void) {
53 // Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
54 const uint32_t value
= random_
.Generate(512);
55 return static_cast<int16_t>(value
) - 256;
59 const uint32_t value
=
60 random_
.Generate(testing::internal::Random::kMaxRange
);
61 // There's a bit more entropy in the upper bits of this implementation.
62 return (value
>> 23) & 0xff;
65 uint8_t Rand8Extremes(void) {
66 // Returns a random value near 0 or near 255, to better exercise
67 // saturation behavior.
68 const uint8_t r
= Rand8();
69 return r
< 128 ? r
<< 4 : r
>> 4;
72 int PseudoUniform(int range
) { return random_
.Generate(range
); }
74 int operator()(int n
) { return PseudoUniform(n
); }
76 static int DeterministicSeed(void) { return 0xbaba; }
79 testing::internal::Random random_
;
82 } // namespace libaom_test
84 #endif // TEST_ACM_RANDOM_H_