Modding:Random Functions: Difference between revisions

Jump to navigation Jump to search
2,530 bytes added ,  03:23, 24 September 2020
Add sample random number generator
(Add sample random number generator)
Line 41: Line 41:


===Random(int low, int high)===
===Random(int low, int high)===
Calls <code>Stat.Rnd().</code>
Calls <code>Stat.Rnd()</code>




===TinkerRandom(int Low, int High)===
===TinkerRandom(int Low, int High)===
Calls <code>Stat.Rnd4()</code>.
Calls <code>Stat.Rnd4()</code>


===GaussianRandom(float Mean, float StandardDeviation)===
===GaussianRandom(float Mean, float StandardDeviation)===
Line 58: Line 58:
===SeededRandom(string Seed, int Low, int High)===
===SeededRandom(string Seed, int Low, int High)===
Returns an int based on <code>Seed</code>, in the range of <code>[Low, High]</code>.
Returns an int based on <code>Seed</code>, in the range of <code>[Low, High]</code>.
== Sample Random Provider for Mod ==
Here is a fully usable out-of-the box random number provider for your mod, which will not interfere with the game's own random seeds.
This template provides:
* a <code>Rand</code> property (direct access to the Random object)
* a <code>Next</code> method that returns a random number in the specified range
It also ties itself to each game's worldseed and automatically resets for new games or save loads.
<syntaxhighlight lang="csharp">
using System;
using XRL;
using XRL.Core;
using XRL.Rules;
namespace MODNAME.Utilities
{
    [HasGameBasedStaticCache]
    public static class MODNAME_Random
    {
        private static Random _rand;
        public static Random Rand
        {
            get
            {
                if (_rand == null)
                {
                    if (XRLCore.Core?.Game == null)
                    {
                        throw new Exception("MODNAME mod attempted to retrieve Random, but Game is not created yet.");
                    }
                    else if (XRLCore.Core.Game.IntGameState.ContainsKey("MODNAME:Random"))
                    {
                        int seed = XRLCore.Core.Game.GetIntGameState("MODNAME:Random");
                        _rand = new Random(seed);
                    }
                    else
                    {
                        _rand = Stat.GetSeededRandomGenerator("MODNAME");
                    }
                    XRLCore.Core.Game.SetIntGameState("MODNAME:Random", _rand.Next());
                }
                return _rand;
            }
        }
        [GameBasedCacheInit]
        public static void ResetRandom()
        {
            _rand = null;
        }
        public static int Next(int minInclusive, int maxInclusive)
        {
            return Rand.Next(minInclusive, maxInclusive + 1);
        }
    }
}
</syntaxhighlight>
=== Example Usage ===
# Save the code above in its own file (for example, <code>MODNAME_Random.cs</code>).
# In your other mod code files, include the following using statment: <code>using MODNAME.Utilities;</code>
# Call the code in one of the ways demonstrated below.
{| class="wikitable"
| Get a random number between 1 and 10 (inclusive):
|-
|
<syntaxhighlight lang="c#">
MODNAME_Random.Next(1, 10);
</syntaxhighlight>
|}
{| class="wikitable"
| Use the <code>Random</code> object directly. For example, supply it to another method:
|-
|
<syntaxhighlight lang="c#">
someList.ShuffleInPlace(MODNAME_Random.Rand);
</syntaxhighlight>
|}


{{Modding Navbox}}
{{Modding Navbox}}


[[Category:Modding]]
[[Category:Modding]]

Navigation menu