Modding:Random Functions

From Caves of Qud Wiki
Jump to: navigation, search
This page is about modding. See the modding overview for an abstract on modding.
As of Patch 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.


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[edit | edit source]

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()[edit | edit source]

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()[edit | edit source]

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

LevelUpRandom()[edit | edit source]

The Random used to determine randomized things at level up.

GetSeededRandomGenerator(string Seed)[edit | edit source]

If you don't want to use the game's seeds in fear of altering worldseed too much, there is a helper function that returns a new random that hashes a new random. This allows your mod to be affected by world seed but does not alter the base game's random number calls.

public static Random GetSeededRandomGenerator(string Seed)
        {
            if (XRLCore.Core.Game == null)
            {
                return new Random();
            }
            return new Random(Hash.String(XRLCore.Core.Game.GetWorldSeed(null) + Seed));
        }

The Seed here should follow best practice conventions, so YourName_YourMod to avoid overlapping conflicts as much as possible.

Returners[edit | edit source]

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

Random(int low, int high)[edit | edit source]

Calls Stat.Rnd().


TinkerRandom(int Low, int High)[edit | edit source]

Calls Stat.Rnd4().

GaussianRandom(float Mean, float StandardDeviation)[edit | edit source]

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;

Returns a random float around the mean with a probability density of a normal distribution.

RandomCosmetic(int low, int high)[edit | edit source]

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

SeededRandom(string Seed, int Low, int High)[edit | edit source]

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