RNG refactor: use in Extractor, only apply rand_init to default_state (#6123)

This commit is contained in:
Philip Dubé 2026-01-10 12:41:56 +00:00 committed by GitHub
parent 39b8aacdef
commit c2152a8713
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 11 additions and 22 deletions

View file

@ -1,9 +1,9 @@
#include "soh/Enhancements/game-interactor/GameInteractor_Hooks.h"
#include "soh/ShipInit.hpp"
#include "soh/ResourceManagerHelpers.h"
extern "C" {
#include "macros.h"
#include "soh/ResourceManagerHelpers.h"
#include "objects/object_link_boy/object_link_boy.h"
#include "objects/object_link_child/object_link_child.h"
extern SaveContext gSaveContext;

View file

@ -1,10 +1,5 @@
#include "random.hpp"
#include <bit>
#include <random>
#include <cassert>
static bool init = false;
uint64_t rando_state = 0;
const uint64_t multiplier = 6364136223846793005ULL;
const uint64_t increment = 11634580027462260723ULL;

View file

@ -7,6 +7,7 @@
#include "Extract.h"
#include "portable-file-dialogs.h"
#include <ship/utils/binarytools/BitConverter.h>
#include "soh/ShipUtils.h"
#include "variables.h"
#ifdef unix
@ -46,7 +47,6 @@
#include <fstream>
#include <filesystem>
#include <unordered_map>
#include <random>
#include <string>
extern "C" uint32_t CRC32C(unsigned char* data, size_t dataSize);
@ -619,13 +619,10 @@ std::string Extractor::Mkdtemp() {
// create 6 random alphanumeric characters
static const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, sizeof(charset) - 1);
char randchr[7];
for (int i = 0; i < 6; i++) {
randchr[i] = charset[dist(gen)];
randchr[i] = charset[ShipUtils::Random(0, sizeof(charset))];
}
randchr[6] = '\0';

View file

@ -98,14 +98,13 @@ extern "C" void* Ship_GetCharFontTexture(u8 character) {
return (void*)fontTbl[adjustedChar];
}
static bool rand_init = false;
static bool default_init = false;
uint64_t default_state = 0;
const uint64_t multiplier = 6364136223846793005ULL;
const uint64_t increment = 11634580027462260723ULL;
// Initialize with seed specified
void ShipUtils::RandInit(uint64_t seed, uint64_t* state) {
rand_init = true;
if (state == nullptr) {
state = &default_state;
}
@ -115,16 +114,16 @@ void ShipUtils::RandInit(uint64_t seed, uint64_t* state) {
uint32_t ShipUtils::next32(uint64_t* state) {
if (state == nullptr) {
state = &default_state;
}
if (!rand_init) {
// No seed given, get a random number from device to seed
if (!default_init) {
// No seed given, get a random number from device to seed
#if !defined(__SWITCH__) && !defined(__WIIU__)
uint64_t seed = static_cast<uint64_t>(std::random_device{}());
uint64_t seed = static_cast<uint64_t>(std::random_device{}());
#else
uint64_t seed = static_cast<uint64_t>(rand());
uint64_t seed = static_cast<uint64_t>(rand());
#endif
ShipUtils::RandInit(seed, state);
default_init = true;
ShipUtils::RandInit(seed, state);
}
}
*state = *state * multiplier + increment;

View file

@ -1,10 +1,8 @@
#include "UIWidgets.hpp"
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui_internal.h>
#include <sstream>
#include <libultraship/libultraship.h>
#include <string>
#include <random>
#include <math.h>
#include <unordered_map>
#include <libultraship/libultra/types.h>