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.

random.h 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Frozen
  3. * Copyright 2016 QuarksLab
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. */
  22. #ifndef FROZEN_LETITGO_RANDOM_H
  23. #define FROZEN_LETITGO_RANDOM_H
  24. #include "frozen/bits/algorithms.h"
  25. #include "frozen/bits/version.h"
  26. #include <cstdint>
  27. #include <type_traits>
  28. namespace frozen {
  29. template <class UIntType, UIntType a, UIntType c, UIntType m>
  30. class linear_congruential_engine {
  31. static_assert(std::is_unsigned<UIntType>::value,
  32. "UIntType must be an unsigned integral type");
  33. public:
  34. using result_type = UIntType;
  35. static constexpr result_type multiplier = a;
  36. static constexpr result_type increment = c;
  37. static constexpr result_type modulus = m;
  38. static constexpr result_type default_seed = 1u;
  39. linear_congruential_engine() = default;
  40. constexpr linear_congruential_engine(result_type s) { seed(s); }
  41. void seed(result_type s = default_seed) { state_ = s; }
  42. constexpr result_type operator()() {
  43. using uint_least_t = bits::select_uint_least_t<bits::log(a) + bits::log(m) + 4>;
  44. uint_least_t tmp = static_cast<uint_least_t>(multiplier) * state_ + increment;
  45. // the static cast below may end up doing a truncation
  46. if(modulus != 0)
  47. state_ = static_cast<result_type>(tmp % modulus);
  48. else
  49. state_ = static_cast<result_type>(tmp);
  50. return state_;
  51. }
  52. constexpr void discard(unsigned long long n) {
  53. while (n--)
  54. operator()();
  55. }
  56. static constexpr result_type min() { return increment == 0u ? 1u : 0u; };
  57. static constexpr result_type max() { return modulus - 1u; };
  58. friend constexpr bool operator==(linear_congruential_engine const &self,
  59. linear_congruential_engine const &other) {
  60. return self.state_ == other.state_;
  61. }
  62. friend constexpr bool operator!=(linear_congruential_engine const &self,
  63. linear_congruential_engine const &other) {
  64. return !(self == other);
  65. }
  66. private:
  67. result_type state_ = default_seed;
  68. };
  69. using minstd_rand0 =
  70. linear_congruential_engine<std::uint_fast32_t, 16807, 0, 2147483647>;
  71. using minstd_rand =
  72. linear_congruential_engine<std::uint_fast32_t, 48271, 0, 2147483647>;
  73. // This generator is used by default in unordered frozen containers
  74. using default_prg_t = minstd_rand;
  75. } // namespace frozen
  76. #endif