betterrandom was not that better

This commit is contained in:
UbitUmarov
2022-10-02 11:42:07 +01:00
parent 2112665133
commit cce98b18c0
4 changed files with 1 additions and 603 deletions

View File

@@ -4530,54 +4530,4 @@ namespace OpenSim.Framework
}
}
*/
public class BetterRandom
{
private const int BufferSize = 1024; // must be a multiple of 4
private byte[] RandomBuffer;
private int BufferOffset;
private RNGCryptoServiceProvider rng;
public BetterRandom()
{
RandomBuffer = new byte[BufferSize];
rng = new RNGCryptoServiceProvider();
BufferOffset = RandomBuffer.Length;
}
private void FillBuffer()
{
rng.GetBytes(RandomBuffer);
BufferOffset = 0;
}
public int Next()
{
if (BufferOffset >= RandomBuffer.Length)
{
FillBuffer();
}
int val = BitConverter.ToInt32(RandomBuffer, BufferOffset) & 0x7fffffff;
BufferOffset += sizeof(int);
return val;
}
public int Next(int maxValue)
{
return Next() % maxValue;
}
public int Next(int minValue, int maxValue)
{
if (maxValue < minValue)
{
throw new ArgumentOutOfRangeException("maxValue must be greater than or equal to minValue");
}
int range = maxValue - minValue;
return minValue + Next(range);
}
public double NextDouble()
{
int val = Next();
return (double)val / int.MaxValue;
}
public void GetBytes(byte[] buff)
{
rng.GetBytes(buff);
}
}
}