From a69f722f0fbef2dd24539d906833c0a9176b6c67 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 26 Jan 2015 14:13:42 -0400 Subject: [PATCH] love.math.setRandomSeed / RandomGenerator:setSeed now hashes the seed value before using it, which gives a much better distribution when comparing the results of different but similar seeds. Cleaned up RandomGenerator:setState/getState, now that love is using the C++11 standard library. --HG-- branch : minor --- src/modules/math/RandomGenerator.cpp | 70 +++++++++++++--------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/src/modules/math/RandomGenerator.cpp b/src/modules/math/RandomGenerator.cpp index 7628c3462..34cda1bc4 100644 --- a/src/modules/math/RandomGenerator.cpp +++ b/src/modules/math/RandomGenerator.cpp @@ -21,11 +21,11 @@ #include "RandomGenerator.h" // C++ -#include #include #include // C +#include #include namespace love @@ -33,6 +33,20 @@ namespace love namespace math { +// Thomas Wang's 64-bit integer hashing function: +// https://web.archive.org/web/20110807030012/http://www.cris.com/%7ETtwang/tech/inthash.htm +static uint64 wangHash64(uint64 key) +{ + key = (~key) + (key << 21); // key = (key << 21) - key - 1; + key = key ^ (key >> 24); + key = (key + (key << 3)) + (key << 8); // key * 265 + key = key ^ (key >> 14); + key = (key + (key << 2)) + (key << 4); // key * 21 + key = key ^ (key >> 28); + key = key + (key << 31); + return key; +} + // 64 bit Xorshift implementation taken from the end of Sec. 3 (page 4) in // George Marsaglia, "Xorshift RNGs", Journal of Statistical Software, Vol.8 (Issue 14), 2003 @@ -75,17 +89,18 @@ double RandomGenerator::randomNormal(double stddev) void RandomGenerator::setSeed(RandomGenerator::Seed newseed) { - // 0 xor 0 is still 0, so Xorshift can't generate new numbers. - if (newseed.b64 == 0) - throw love::Exception("Random seed cannot be 0."); - seed = newseed; - rng_state = seed; - // Xorshift's first couple results after seeding will be similar to results - // from very similar seeds, so we immediately discard them here. - for (int i = 0; i < 2; i++) - rand(); + // Xorshift isn't designed to give a good distribution of values across many + // similar seeds, so we hash the state integer before using it. + // http://www.reedbeta.com/blog/2013/01/12/quick-and-easy-gpu-random-numbers-in-d3d11/ + // Xorshift also can't handle a state value of 0, so we avoid that. + do + { + newseed.b64 = wangHash64(newseed.b64); + } while (newseed.b64 == 0); + + rng_state = newseed; } RandomGenerator::Seed RandomGenerator::getSeed() const @@ -98,32 +113,17 @@ void RandomGenerator::setState(const std::string &statestr) // For this implementation we'll accept a hex string representing the // 64-bit state integer xorshift uses. - Seed state = {}; - // Hex string must start with 0x. if (statestr.find("0x") != 0 || statestr.size() < 3) - throw love::Exception("Invalid random state."); + throw love::Exception("Invalid random state: %s", statestr.c_str()); - // standardized strtoull (or 64 bit integer support for stringstream) - // requires C++11's standard library, which we can't use yet. - // I use strtol like this not because it's the best solution, but because - // it's "good enough". + Seed state = {}; - // Convert the hex string to the state integer character-by-character. - for (size_t i = 2; i < statestr.size(); i++) - { - char hex[2] = {statestr[i], 0}; - char *end = nullptr; + char *end = nullptr; + state.b64 = strtoull(statestr.c_str(), &end, 16); - // Convert the current hex character to a number. - int nibble = strtol(hex, &end, 16); - - // Check if strtol failed to convert it. - if (end != nullptr && *end != 0) - throw love::Exception("Invalid random state."); - - state.b64 = (state.b64 << 4) + nibble; - } + if (end != nullptr && *end != 0) + throw love::Exception("Invalid random state: %s", statestr.c_str()); rng_state = state; } @@ -132,14 +132,8 @@ std::string RandomGenerator::getState() const { // For this implementation we'll return a hex string representing the 64-bit // state integer xorshift uses. - std::stringstream ss; - - ss << "0x"; - - // Again with the stringstream not dealing with 64 bit integers... - ss << std::setfill('0') << std::setw(8) << std::hex << rng_state.b32.high; - ss << std::setfill('0') << std::setw(8) << std::hex << rng_state.b32.low; + ss << "0x" << std::setfill('0') << std::setw(16) << std::hex << rng_state.b64; return ss.str(); }