A small tweak to lua random

This commit is contained in:
2022-04-06 15:09:28 -04:00
parent 89365ea9ba
commit 453809b65c
3 changed files with 18 additions and 4 deletions

View File

@@ -412,12 +412,15 @@ LuaDefine(math_random, "(args...)",
}
}
// Generate the hash and convert to a lua_Number.
uint64_t hash = util::hash_ints(seed, count, salt, 456);
if (!have_range) {
double result = (hash & LuaStack::MAXINT) * 0x1p-53;
lua_pushnumber(L, result);
// Generate the hash and convert to a double.
uint64_t hash = util::hash_ints(seed, count, salt, 456);
lua_pushnumber(L, util::hash_to_double(hash));
} else {
// Generate the hash and scale it into the desired range.
// This code is not quite right: the results are not quite
// uniform, this is especially true for very long ranges.
uint64_t hash = util::hash_ints(seed, count, salt, 456);
uint64_t range = (high - low) + 1;
uint64_t offset = (hash & 0x7FFFFFFFFFFFFFFF) % range;
int64_t result = low + int64_t(offset);