Guest User

Untitled

a guest
May 26th, 2018
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <chrono>
  2. #include <iostream>
  3. #include <map>
  4.  
  5. namespace Random {
  6. class LCG {
  7. static constexpr uint64_t const A = 0x5851F42D4C957F2D;
  8. static constexpr uint64_t const C = 0x14057B7EF767814F;
  9. static constexpr uint64_t const M = 0xFFFFFFFFFFFFFFFF;
  10.  
  11. uint64_t this_seed;
  12.  
  13. auto now() noexcept {
  14. using namespace std::chrono;
  15. auto const output = high_resolution_clock::now();
  16. return output.time_since_epoch().count();
  17. }
  18.  
  19. public:
  20. LCG() noexcept
  21. : this_seed(now()) {
  22. }
  23.  
  24. LCG(uint64_t const value) noexcept
  25. : this_seed(value) {
  26. }
  27.  
  28. void seed() noexcept {
  29. this_seed = now();
  30. }
  31.  
  32. void seed(uint64_t const value) noexcept {
  33. this_seed = value;
  34. }
  35.  
  36. // [0, 2 ^ 64 - 1)
  37. auto next() noexcept {
  38. this_seed = (this_seed * A + C) & M;
  39. return this_seed;
  40. }
  41.  
  42. void discard(uint64_t const amount) noexcept {
  43. for (uint64_t i = 0; i != amount; ++i) {
  44. next();
  45. }
  46. }
  47.  
  48. // [0, 1)
  49. double get() noexcept {
  50. return static_cast<double>(next()) / M;
  51. }
  52.  
  53. // x == (-1 | 0 | 1) ? 0 : x > 0 ? [0, x) : (x, 0]
  54. int64_t get(int64_t const x) noexcept {
  55. return static_cast<int64_t>(get() * x);
  56. }
  57.  
  58. // [a, b)
  59. int64_t get(int64_t const a, int64_t const b) noexcept {
  60. return a + static_cast<int64_t>(get() * (b - a));
  61. }
  62. };
  63. }
  64.  
  65. int main() {
  66.  
  67. Random::LCG lcg(0);
  68. std::map<int64_t, uint64_t> buckets;
  69. for (int i = 0; i != 1000000; ++i) {
  70. ++buckets[lcg.get(50)];
  71. }
  72. for (auto const [a, b] : buckets) {
  73. std::cout << a << 't' << b << 'n';
  74. }
  75.  
  76. return 0;
  77. }
RAW Paste Data