Skip to content

Custom RNG

Ricky Setiawan edited this page Jul 12, 2022 · 1 revision

You can create your own RNG. It's easy to set up.

Here's the steps:

1. Inherit Random32 or Random64.

public class LitdexRNG : Random64
{

}

2. Add a member or field to store the seed.

public class LitdexRNG : Random64
{
    private ulong _seed;
}

3. Add constructor and or destructor.

public class LitdexRNG : Random64
{
    private ulong _seed;

    public LitdexRNG(ulong seed)
    {
        this._seed = seed;
    }
}

4. Override the Next() method. This is the internal state of RNG's and the place you implement your algorithm.

public class LitdexRNG : Random64
{
    private ulong _seed;

    public LitdexRNG(ulong seed)
    {
        this._seed = seed;
    }

    protected override ulong Next()
    {
        var a = (this._seed >> 11) | (this._seed << (32 - 11));
        this._seed = a * 951879;
        return this._seed;
    }
}

5. Don't forget to override AlgorithmName method or the name will be base class.

public class LitdexRNG : Random64
{
    private ulong _seed;

    public LitdexRNG(ulong seed)
    {
        this._seed = seed;
    }

    protected override ulong Next()
    {
        var a = (this._seed >> 11) | (this._seed << (32 - 11));
        this._seed = a * 951879;
        return this._seed;
    }

    public override string AlgorithmName()
    {
        return "LitdexRNG";
    }
}

6. Override the other method if you want to change the algorithm.