distribution.cxx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // distribution.cpp
  3. // shared_random_generator
  4. //
  5. // Created by Sam Jaffe on 3/25/23.
  6. // Copyright © 2023 Sam Jaffe. All rights reserved.
  7. //
  8. #include "random/distribution.h"
  9. #include <iostream>
  10. #include <random>
  11. #include "random/device.h"
  12. namespace engine::random {
  13. template <typename Rand>
  14. Uniform<Rand>::Uniform(Rand min)
  15. : Uniform(min, std::numeric_limits<Rand>::max()) {}
  16. template <typename Rand>
  17. Uniform<Rand>::Uniform(Rand min, Rand max) : min_(min), max_(max) {}
  18. template <typename Rand>
  19. auto Uniform<Rand>::operator()(Device & device) const -> result_type {
  20. if constexpr (std::is_floating_point_v<Rand>) {
  21. return std::uniform_real_distribution<Rand>(min_, max_)(device);
  22. } else {
  23. return std::uniform_int_distribution<Rand>(min_, max_)(device);
  24. }
  25. }
  26. template <typename Rand> void Uniform<Rand>::describe(std::ostream & os) const {
  27. constexpr char close = std::is_floating_point_v<Rand> ? ')' : ']';
  28. os << "Uniform[" << min_ << ',' << max_ << close;
  29. }
  30. template class Uniform<size_t>;
  31. template class Uniform<double>;
  32. template class Uniform<int32_t>;
  33. template class Uniform<uint32_t>;
  34. }