RandomGenerator
RandomGenerator returns a set of random _values in a repeatable
way, to allow re-running of tests if necessary.
This class is internal to the framework but exposed externally to through the TestContext
the class is used to allow for obtaining repeatable random _values during a tests execution.
this class should not be used inside the framework only with a TestMethod.
using System;
namespace NUnit.Framework.Internal
{
public class RandomGenerator
{
public readonly int seed;
private Random random;
private Random Rand {
get {
random = ((random == null) ? new Random(seed) : random);
return random;
}
}
public RandomGenerator(int seed)
{
this.seed = seed;
}
public int GetInt()
{
return Rand.Next();
}
public int GetInt(int min, int max)
{
return Rand.Next(min, max);
}
public short GetShort()
{
return (short)Rand.Next(-32768, 32767);
}
public short GetShort(short min, short max)
{
return (short)Rand.Next(min, max);
}
public byte GetByte()
{
return (byte)Rand.Next(0, 255);
}
public byte GetByte(byte min, byte max)
{
return (byte)Rand.Next(min, max);
}
public bool GetBool()
{
return Rand.Next(0, 2) == 0;
}
public bool GetBool(double probability)
{
return Rand.NextDouble() < Math.Abs(probability % 1);
}
public double GetDouble()
{
return Rand.NextDouble();
}
public float GetFloat()
{
return (float)Rand.NextDouble();
}
public T GetEnum<T>()
{
Array enumValues = TypeHelper.GetEnumValues(typeof(T));
return (T)enumValues.GetValue(Rand.Next(0, enumValues.Length));
}
}
}