Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef CHRE_WIFI_OFFLOAD_TEST_RANDOM_GENERATOR_H_
     18 #define CHRE_WIFI_OFFLOAD_TEST_RANDOM_GENERATOR_H_
     19 
     20 #include <cinttypes>
     21 #include <random>
     22 
     23 namespace wifi_offload_test {
     24 
     25 /**
     26  * @class RandomGenerator A class to generate random values for any uint type:
     27  *        (uint8_t, ..., uint64_t). Also supports resetting to its initial
     28  *        state to be able to reproduce the same random sequence.
     29  */
     30 class RandomGenerator {
     31  public:
     32   /**
     33    * Default constructs a RandomGenerator object
     34    */
     35   RandomGenerator();
     36 
     37   /**
     38    * Resets the object to its initial state. Useful if we want to reproduce the
     39    * exact same sequenct again.
     40    */
     41   void Reset();
     42 
     43   /**
     44    * Generates a random number of type IntType
     45    *
     46    * @return A random number of type IntType
     47    */
     48   template <typename IntType>
     49   IntType get() {
     50     return static_cast<IntType>(uniform_distribution_(random_engine_));
     51   }
     52 
     53  private:
     54   /* The initial seed kept to use again when need to reset  */
     55   std::random_device::result_type initial_seed_;
     56 
     57   /* Standard mersenne_twister_engine */
     58   std::mt19937 random_engine_;
     59 
     60   /**
     61    * Use uniform_distribution_ to transform the random unsigned int generated by
     62    * random_engine_ into an uint64_t
     63    */
     64   std::uniform_int_distribution<uint64_t> uniform_distribution_;
     65 };
     66 
     67 }  // wifi_offload_test namespace
     68 
     69 #endif  // CHRE_WIFI_OFFLOAD_TEST_RANDOM_GENERATOR_H_
     70