Is any floating point math used in random.NextInt(min, max)?

Just straightforward question:
Is any floating point math used in random.NextInt(min, max)?

I’m asking because Id like to generate a random number and use it in a deterministic lockstep multiplayer setup.
So I’d like to know if I can rely on the determinism of the call despite potentially different CPU architectures making the call. (assuming they are using the same seed)

Thanks.

As long as you use the same seed and make the same sequence of calls with the same parameters, you will get the same sequence of random numbers back.

This is the Unity.Mathematics package right? You do know that this means you have all the code on your computer so can easily look through it.

Look in your project in “Library/PackageCache/com.unity.mathematics@/Unity.Mathematics/random.cs”

public int NextInt()
{
    return (int)NextState() ^ -2147483648;
}

public int NextInt(int min, int max)
{
    CheckNextIntMinMax(min, max);
    uint range = (uint)(max - min);
    return (int)(NextState() * (ulong)range >> 32) + min;
}

private uint NextState()
{
    CheckState();
    uint t = state;
    state ^= state << 13;
    state ^= state >> 17;
    state ^= state << 5;
    return t;
}
3 Likes

Oh perfect!
I was not aware. Thanks

1 Like