Modding:Random Functions

From Caves of Qud Wiki
Revision as of 03:02, 24 June 2019 by Teamtoto (talk | contribs) (Some random functions' purposes are still unknown.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
This information is reliable as of patch 2.0.182.1. If this is no longer the current patch, you can help by updating it.
As of Patch This information is reliable as of patch 2.0.182.1.
This article is a stub. You can help Caves of Qud Wiki by expanding it.
This article is a stub. You can help Caves of Qud Wiki by expanding it.

If your mod has some element of randomness, you're gonna be looking for a function to call. However, calling the built in Next() function in the Random Class built into C# will net you some compilation errors. The best practice is to use the random functions inside XRL/Rules/Stat.cs. Below are the different random functions inside Caves of Qud and what they are used for.

Randoms

These are the methods that return the Random class. These are all hashed from

      Stat.Rand = new Random(Hash.String("Seed0" + (object) Seed));
      Stat.Rnd = new Random(Hash.String("Seed1" + (object) Seed));
      Stat.Rnd2 = new Random(Hash.String("Seed2" + (object) Seed));
      if (includeLifetimeSeeds)
        Stat.LevelUpRandom = new Random(Hash.String("Seed3" + (object) Seed));
      Stat.Rnd4 = new Random(Hash.String("Seed4" + (object) Seed));
      Stat.Rnd5 = new Random(Hash.String("Seed5" + (object) Seed));

Rnd()

This is the main function that randomizes and determines things such as sultan artifacts, villages, and basically everything important that is based on world seed. All effects and mutations use this function. Don't use this function in your mod if you don't want to ruin world seeds.

Rnd2(), Rnd4(), Rnd5()

These functions deals with the smaller events you find in Qud.

LevelUpRandom()

The Random used to determine randomized things at level up.

Returners

These use a specific random class to return a multitude of things.

Random(int low, int high)

Calls Stat.Rnd().


TinkerRandom(int Low, int High)

Calls Stat.Rnd4().

GaussianRandom(float Mean, float StandardDeviation)

double num = Math.Sqrt(-2.0 * Math.Log(Stat.Rnd.NextDouble())) * Math.Sin(2.0 * Math.PI * Stat.Rnd.NextDouble());
      return (double) Mean + (double) StandardDeviation * num;

RandomCosmetic(int low, int high)

Used most often when getting random angles for particles and other cosmetic effects. Calls Stat.Rnd2().

SeededRandom(string Seed, int Low, int High)

Returns an int based on Seed, in the range of [Low, High].