2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
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.
11 #ifndef VPX_TEST_ACM_RANDOM_H_
12 #define VPX_TEST_ACM_RANDOM_H_
18 #include "third_party/googletest/src/include/gtest/gtest.h"
20 #include "vpx/vpx_integer.h"
22 namespace libvpx_test {
26 ACMRandom() : random_(DeterministicSeed()) {}
28 explicit ACMRandom(int seed) : random_(seed) {}
30 void Reset(int seed) { random_.Reseed(seed); }
31 uint16_t Rand16(void) {
32 const uint32_t value =
33 random_.Generate(testing::internal::Random::kMaxRange);
34 return (value >> 15) & 0xffff;
37 int32_t Rand20Signed(void) {
38 // Use 20 bits: values between 524287 and -524288.
39 const uint32_t value = random_.Generate(1048576);
40 return static_cast<int32_t>(value) - 524288;
43 int16_t Rand16Signed(void) {
44 // Use 16 bits: values between 32767 and -32768.
45 const uint32_t value = random_.Generate(65536);
46 return static_cast<int16_t>(value) - 32768;
49 int16_t Rand13Signed(void) {
50 // Use 13 bits: values between 4095 and -4096.
51 const uint32_t value = random_.Generate(8192);
52 return static_cast<int16_t>(value) - 4096;
55 int16_t Rand9Signed(void) {
56 // Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
57 const uint32_t value = random_.Generate(512);
58 return static_cast<int16_t>(value) - 256;
62 const uint32_t value =
63 random_.Generate(testing::internal::Random::kMaxRange);
64 // There's a bit more entropy in the upper bits of this implementation.
65 return (value >> 23) & 0xff;
68 uint8_t Rand8Extremes(void) {
69 // Returns a random value near 0 or near 255, to better exercise
70 // saturation behavior.
71 const uint8_t r = Rand8();
72 return r < 128 ? r << 4 : r >> 4;
75 uint32_t RandRange(const uint32_t range) {
76 // testing::internal::Random::Generate provides values in the range
77 // testing::internal::Random::kMaxRange.
78 assert(range <= testing::internal::Random::kMaxRange);
79 return random_.Generate(range);
82 int PseudoUniform(int range) { return random_.Generate(range); }
84 int operator()(int n) { return PseudoUniform(n); }
86 static int DeterministicSeed(void) { return 0xbaba; }
89 testing::internal::Random random_;
92 } // namespace libvpx_test
94 #endif // VPX_TEST_ACM_RANDOM_H_