Skip to content

Commit

Permalink
Remove Random::SFixed, improve Random::NormFixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sturnclaw committed Oct 7, 2023
1 parent c09da89 commit c94fe57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
24 changes: 11 additions & 13 deletions src/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
11 changes: 6 additions & 5 deletions src/galaxy/StarSystemGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit c94fe57

Please sign in to comment.