| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #include "RandomGenerator.h" |
| 22 | |
| 23 | // C++ |
| 24 | #include <sstream> |
| 25 | #include <iomanip> |
| 26 | |
| 27 | // C |
| 28 | #include <cmath> |
| 29 | #include <cstdlib> |
| 30 | |
| 31 | namespace love |
| 32 | { |
| 33 | namespace math |
| 34 | { |
| 35 | |
| 36 | // Thomas Wang's 64-bit integer hashing function: |
| 37 | // https://web.archive.org/web/20110807030012/http://www.cris.com/%7ETtwang/tech/inthash.htm |
| 38 | static uint64 wangHash64(uint64 key) |
| 39 | { |
| 40 | key = (~key) + (key << 21); // key = (key << 21) - key - 1; |
| 41 | key = key ^ (key >> 24); |
| 42 | key = (key + (key << 3)) + (key << 8); // key * 265 |
| 43 | key = key ^ (key >> 14); |
| 44 | key = (key + (key << 2)) + (key << 4); // key * 21 |
| 45 | key = key ^ (key >> 28); |
| 46 | key = key + (key << 31); |
| 47 | return key; |
| 48 | } |
| 49 | |
| 50 | love::Type RandomGenerator::type("RandomGenerator" , &Object::type); |
| 51 | |
| 52 | // 64 bit Xorshift implementation taken from the end of Sec. 3 (page 4) in |
| 53 | // George Marsaglia, "Xorshift RNGs", Journal of Statistical Software, Vol.8 (Issue 14), 2003 |
| 54 | // Use an 'Xorshift*' variant, as shown here: http://xorshift.di.unimi.it |
| 55 | |
| 56 | RandomGenerator::RandomGenerator() |
| 57 | : last_randomnormal(std::numeric_limits<double>::infinity()) |
| 58 | { |
| 59 | // because it is too big for some compilers to handle ... if you know what |
| 60 | // i mean |
| 61 | Seed newseed; |
| 62 | newseed.b32.low = 0xCBBF7A44; |
| 63 | newseed.b32.high = 0x0139408D; |
| 64 | setSeed(newseed); |
| 65 | } |
| 66 | |
| 67 | uint64 RandomGenerator::rand() |
| 68 | { |
| 69 | rng_state.b64 ^= (rng_state.b64 >> 12); |
| 70 | rng_state.b64 ^= (rng_state.b64 << 25); |
| 71 | rng_state.b64 ^= (rng_state.b64 >> 27); |
| 72 | return rng_state.b64 * 2685821657736338717ULL; |
| 73 | } |
| 74 | |
| 75 | // Box–Muller transform |
| 76 | double RandomGenerator::randomNormal(double stddev) |
| 77 | { |
| 78 | // use cached number if possible |
| 79 | if (last_randomnormal != std::numeric_limits<double>::infinity()) |
| 80 | { |
| 81 | double r = last_randomnormal; |
| 82 | last_randomnormal = std::numeric_limits<double>::infinity(); |
| 83 | return r * stddev; |
| 84 | } |
| 85 | |
| 86 | double r = sqrt(-2.0 * log(1. - random())); |
| 87 | double phi = 2.0 * LOVE_M_PI * (1. - random()); |
| 88 | |
| 89 | last_randomnormal = r * cos(phi); |
| 90 | return r * sin(phi) * stddev; |
| 91 | } |
| 92 | |
| 93 | void RandomGenerator::setSeed(RandomGenerator::Seed newseed) |
| 94 | { |
| 95 | seed = newseed; |
| 96 | |
| 97 | // Xorshift isn't designed to give a good distribution of values across many |
| 98 | // similar seeds, so we hash the state integer before using it. |
| 99 | // http://www.reedbeta.com/blog/2013/01/12/quick-and-easy-gpu-random-numbers-in-d3d11/ |
| 100 | // Xorshift also can't handle a state value of 0, so we avoid that. |
| 101 | do |
| 102 | { |
| 103 | newseed.b64 = wangHash64(newseed.b64); |
| 104 | } while (newseed.b64 == 0); |
| 105 | |
| 106 | rng_state = newseed; |
| 107 | |
| 108 | last_randomnormal = std::numeric_limits<double>::infinity(); |
| 109 | } |
| 110 | |
| 111 | RandomGenerator::Seed RandomGenerator::getSeed() const |
| 112 | { |
| 113 | return seed; |
| 114 | } |
| 115 | |
| 116 | void RandomGenerator::setState(const std::string &statestr) |
| 117 | { |
| 118 | // For this implementation we'll accept a hex string representing the |
| 119 | // 64-bit state integer xorshift uses. |
| 120 | |
| 121 | // Hex string must start with 0x. |
| 122 | if (statestr.find("0x" ) != 0 || statestr.size() < 3) |
| 123 | throw love::Exception("Invalid random state: %s" , statestr.c_str()); |
| 124 | |
| 125 | Seed state = {}; |
| 126 | |
| 127 | char *end = nullptr; |
| 128 | state.b64 = strtoull(statestr.c_str(), &end, 16); |
| 129 | |
| 130 | if (end != nullptr && *end != 0) |
| 131 | throw love::Exception("Invalid random state: %s" , statestr.c_str()); |
| 132 | |
| 133 | rng_state = state; |
| 134 | |
| 135 | last_randomnormal = std::numeric_limits<double>::infinity(); |
| 136 | } |
| 137 | |
| 138 | std::string RandomGenerator::getState() const |
| 139 | { |
| 140 | // For this implementation we'll return a hex string representing the 64-bit |
| 141 | // state integer xorshift uses. |
| 142 | std::stringstream ss; |
| 143 | ss << "0x" << std::setfill('0') << std::setw(16) << std::hex << rng_state.b64; |
| 144 | |
| 145 | return ss.str(); |
| 146 | } |
| 147 | |
| 148 | } // math |
| 149 | } // love |
| 150 | |