From c94fe57825059e6eb115f8578a3b69432adef3d5 Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Fri, 6 Oct 2023 22:50:19 -0400 Subject: [PATCH] Remove Random::SFixed, improve Random::NormFixed --- src/Random.h | 24 +++++++++++------------- src/galaxy/StarSystemGenerator.cpp | 11 ++++++----- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Random.h b/src/Random.h index 862468e4063..ea94dc7d82a 100644 --- a/src/Random.h +++ b/src/Random.h @@ -246,23 +246,21 @@ class Random : public RefCounted { return o; } - // interval (-1,1) - // triangle distribution at p=1 - // increasing steepness of normal distribution at p > 1 - inline fixed SFixed(int p) + // Returns an approximation of a normal distribution in the bounded interval + // (-1, 1) + inline fixed NormFixed() { - fixed o = Fixed(); - o -= Fixed(); - while (--p > 0) - o *= (fixed(1, 4) + Fixed() * fixed(3, 4)); - return o; + // Because addition is fully commutative, the compiler can order these + // Fixed() calls in any order it wants without changing the result + fixed o = Fixed() + Fixed() + Fixed(); + return o * fixed(10, 15) - fixed(1, 1); } - // interval (mean - stddev, mean + stddev) - // this is not a true gaussian distribution - inline fixed NormFixed(fixed mean, fixed stddev) + // interval (mean - maxdev, mean + maxdev) + // this is an approximation of a gaussian distribution with cross-platform determinism + inline fixed NormFixed(fixed mean, fixed maxdev) { - return mean + SFixed(2) * stddev; + return mean + maxdev * NormFixed(); } const pcg32 &GetPCG() const { return mPCG; } diff --git a/src/galaxy/StarSystemGenerator.cpp b/src/galaxy/StarSystemGenerator.cpp index 654d9bd160c..1127ef1c316 100644 --- a/src/galaxy/StarSystemGenerator.cpp +++ b/src/galaxy/StarSystemGenerator.cpp @@ -365,7 +365,7 @@ fixedf<48> StarSystemLegacyGeneratorBase::CalcHillRadius(SystemBody *sbody) cons using fixedp = fixedf<48>; if (sbody->GetSuperType() <= SystemBody::SUPERTYPE_STAR) { - return fixed(); + return fixedp(); } else { // playing with precision since these numbers get small // masses in earth masses @@ -905,7 +905,7 @@ fixed StarSystemRandomGenerator::CalcBodySatelliteShellDensity(Random &rand, Sys discMax = 100 * rand.NFixed(2); // rand-splitting again discMax *= fixed::SqrtOf(fixed(1, 2) + fixed(8, 1) * rand.Fixed()); } else { - discMax = 100 * rand.SFixed(2).Abs() * fixed::SqrtOf(primary->GetMassAsFixed()); + discMax = 100 * rand.NormFixed().Abs() * fixed::SqrtOf(primary->GetMassAsFixed()); } // having limited discMin by bin-separation/fake roche, and @@ -1063,7 +1063,7 @@ SystemBody *StarSystemRandomGenerator::MakeBodyInOrbitSlice(Random &rand, StarSy } // periapsis, apoapsis = closest, farthest distance in orbit - fixed periapsis = min_slice + slice_bump * rand.SFixed(2).Abs(); + fixed periapsis = min_slice + slice_bump * rand.NormFixed().Abs(); if (periapsis > discMax) return nullptr; @@ -1124,9 +1124,10 @@ SystemBody *StarSystemRandomGenerator::MakeBodyInOrbitSlice(Random &rand, StarSy // longitude of ascending node planet->m_orbitalOffset = rand.Fixed() * 2 * FIXED_PI; // inclination in the hemisphere above the equator, low probability of high-inclination orbits - planet->m_inclination = rand.SFixed(5).Abs() * FIXED_PI * fixed(1, 2); + fixed incl_scale = rand.Fixed() * fixed(666, 1000); + planet->m_inclination = rand.NormFixed().Abs() * incl_scale * FIXED_PI * fixed(1, 2); // argument of periapsis, interval -PI .. PI - planet->m_argOfPeriapsis = rand.SFixed(1) * FIXED_PI; + planet->m_argOfPeriapsis = rand.NormFixed() * FIXED_PI; // rare chance of reversed orbit if (rand.Fixed() < fixed(1, 20))