Stardew Valley
0 of 0

File information

Last updated

Original upload

Created by

pepoluan

Uploaded by

pepoluan

Virus scan

Safe to use

About this mod

This mod replaces Stardew Valley's 'problematic' PRNG with a much better one.

Requirements
Permissions and credits
Changelogs
Donations
Donation Points (DP) goes 100% to "Doctors without Borders" (Médecins sans Frontières)

Background


Stardew Valley uses .Net Framework's own System.Random class, a "really bad" PRNG for the several reasons:
  • It's slow
  • It's "biased" towards lower values
  • It has uneven distribution of odd-vs-even numbers
  • It is, in general, broken in the worst ways
  • Here are some more docs, if you really want to delve into the nitty-gritty stuff: [1] [2] [3]

Zoryn4163 created the "Better RNG" mod (available only via GitHub) that tried to fix this bug. Unfortunately, there are several problems with "Better RNG":
  • "Better RNG" uses Mersenne Twister, a slow and kind-of-problematic algorithm. See this paper for even more details.
  • Although the feature is called "Override Daily Luck", it actually triggers only on SaveLoaded.
  • It has recently (as of 2021-05-16) come to my attention that every now and then, the game re-creates System.Random and inject that into Game1.random, meaning that RNG replacement needs to be done continually, something "Better RNG" does not do.
  • The mod has not been updated for more than 1 year.


Benefits of this mod against "Better RNG":
  • This mod uses the much faster and less-problematic XoShiRo128** PRNG, for better randomness. (If you're using SDV64, it will use XoShiRo256** instead, which is even faster for pure 64-bit programs).

    (FYI, the "Xoshiro Family" of PRNG has been selected as the default RNG for Julia 1.6 and .Net 6.0)
  • This mod implements "Override Daily Luck" properly.
  • This mod continually checks if Game1.random had been replaced by a 'rogue' RNG, and enforces its PRNG. This is done in an optimized way that does not cause any lags (technical explanation: PRNG is not re-instantiated since it's cached; mod simply "repoints" Game1.random to the cached PRNG)
  • This mod is regularly updated.


("Override Weather" is NOT implemented because the SDV's logic for weather is quite complex and I don't want to break anyone's game. If you really want to change the odds for weather, you can use another mod such as "Climates of Ferngill".)

Since v1.1.0, you can press F5 to reload changes in config.json (for example, if you don't like the mod's default "Gaussian distribution" for Luck.) However, in later versions you can change the mod's settings wholly via Generic Mod Config Menu.


Installation
  • Install latest SMAPI (mod requires minimal version 3.7.0)
  • Download the provided Zip file and extract into the Mods directory
  • Verify it's running on the SMAPI console
  • (Optional) Configure to your liking by editing config.json or using Generic Mod Config Menu



config.json knobs

  • ReloadHotkey -- Hotkey to press if you want to reload config.json. Default = "RightShift + F5"
  • OverrideDailyLuck -- If true, then everytime a savegame is loaded, you (or your team's) luck will be recalculated
  • GaussianLuck -- If true, then use Gaussian / Normal distribution for luck calculation
  • GaussianLuckStdDev -- Affects the Gaussian / Normal distribution

Note that starting version 1.9.0, it is no longer possible to specify the RNG's seed. This is because since version 1.8.4 the RNG is actually no longer predictable anymore even if you specify a seed, as on each DayStarted it will advance by an unpredictable number (from the OS's secure CSPRNG). Therefore, the ability to specify a seed (and to reseed the currently-running RNG) is removed.

This mod supports configuration using "Generic Mod Config Menu" (GMCM), but does not require GMCM to function.


Note on Gaussian
Gaussian distribution, a.k.a. "Normal distribution", a.k.a. "Bell Curve distribution", means that Luck will tend towards "neutral", sometimes "a bit (un)lucky", and rarely "very (un)lucky". Kinda reflective of Real Life :-)

If you don't like this, set GaussianLuck to false


Some more Technical Details
This mod uses the free/libre library XoshiroPRNG.Net made by yours truly and published using NuGet. It's a drop-in replacement for .Net Framework's System.Random, yet also provide additional interface that fully "unleashes" the power of the Xoshiro family of PRNG. It's suprisingly popular: As of this writing, it has been downloaded more than 2300 times since its release. Someone else had in fact recommended the use of my library over Mersenne Twister or (*shivers*) System.Random.

Source code for the XoshiroPRNG.Net library is available by following the link provided on the NuGet link above.


API for Other Modders
Starting version 1.5.0, "Even Better RNG" also provides an API which other SMAPI modders can use.

The Interface file is provided in the latest version's zipfile. Copy that file to your project, and add using EvenBetterRNG; on the top. I've added XML comments to each interface method, so hopefully you can easily understand what each method does.

Grab the API as per SMAPI instructions, and you will have access to the following functions:
  • GetNewRandom method (with 3 overrides), which will give you a new XoShiRo128** PRNG whenever invoked. Don't keep calling this, you might cause your mod to have memory leak. Rather, call once and cache. Or use the next method:
  • GetNamedRandom method (2 overrides), which will give you a singleton-like PRNG (created only if necessary, exact same one if already created).
  • TryRegisterDailyLuckOverrideCallback, use this to register one (or more) callback(s). The callback(s) will be called after EBRNG has finished recalculating Daily Luck. Use this to ensure that whatever manipulation you're going to do with Daily Luck will not be overwritten by EBRNG.



Source Code
Is available on SourceForge <== Click

Please do not ask me to move the repository to GitHub; that won't ever happen.