Hi,
I want to repeat a random number sequence generated by a legacy software using the VBMath.Rnd and VBMath.Randomize functions in VB .NET
Reading on the documentation for those functions on MSDN i found that you are supposed to "reset" the generator calling Rnd with a negative value if you want the same seed to give you the same result sequence each time.
But doing some tests... things didn't work as expected.
The legacy software does something like this at the start of the application on different executions:
float[] rNums = new float[4];
VBMath.Randomize(154341.77394338892);
for (int index = 0; index < 4; index++)
{
rNums[index] = VBMath.Rnd();
}
And my code does something like this:
VBMath.Rnd(-1);
VBMath.Randomize(154341.77394338892);
for (int index = 0; index < 4; index++)
{
Console.WriteLine("rNum[" + index + "] " + rNums[index] + " = " + VBMath.Rnd());
}
The results for this test are:
rNum[0] 0,6918146 = 0,2605162
rNum[1] 0,5121228 = 0,4748411
rNum[2] 0,8309224 = 0,8112976
rNum[3] 0,972851 = 0,8011347
The sequence that i want to reproduce in the second code any number of times is the sequence generated from the hard coded initial state of the generator. That means the sequence you would get if you run the first code alone.
I can not change the first code.
Any idea on why the VBMath.Rnd and VBMath.Randomize functions arent working as expected?
Did i miss something?
ANSWER
Thee problem is that since the legacy code doesn't call Rnd with a negative value, the generator doesn't clear its state and the call to Rnd gets chained to the previous value of the seed (in this case, the hard-coded value).
To solve the problem and be able to repeat the process all over again without all the problems that would imply "reproducing" the initial state, i cloned the generator code and patched it so i could reproduce the same situation every time depending on a parameter.
I know.. its ugly.. but it solves my problem (Btw i also know that there are some rounding errors and that the generated values are not exact.. they differ in like the last digit or something) but i don't need exact precision.
The rounding error probably comes from my choice of language for the cloning of the algorithm. If someone could help out on how to get the exact same result (match the rounding errors) that would be nice.
The patched code follows.
public sealed class RndGenerator
{
static int m_rndSeed = 0x50000;
// This is the value that the programmer sets the seed at ProjectData object
// initialization
const int CONSTANT_INIT_RNDSEED = 0x50000;
// Methods
private static float GetTimer()
{
DateTime now = DateTime.Now;
return (float)(((((60 * now.Hour) + now.Minute) * 60) + now.Second) + (((double)now.Millisecond) / 1000.0));
}
public static void Randomize()
{
float timer = GetTimer();
int rndSeed = m_rndSeed;
int num = BitConverter.ToInt32(BitConverter.GetBytes(timer), 0);
num = ((num & 0xffff) ^ (num >> 0x10)) << 8;
rndSeed = (rndSeed & -16776961) | num;
m_rndSeed = rndSeed;
}
public static void Randomize(double Number)
{
Randomize(Number, false);
}
public static void Randomize(double Number, bool useHardCodedState)
{
int num;
int rndSeed = 0;
if (useHardCodedState)
rndSeed = CONSTANT_INIT_RNDSEED;
else
rndSeed = m_rndSeed;
if (BitConverter.IsLittleEndian)
{
num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 4);
}
else
{
num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
}
num = ((num & 0xffff) ^ (num >> 0x10)) << 8;
rndSeed = (rndSeed & -16776961) | num;
m_rndSeed = rndSeed;
}
public static float Rnd()
{
return Rnd(1f);
}
public static float Rnd(float Number)
{
int rndSeed = m_rndSeed;
if (Number != 0.0)
{
if (Number < 0.0)
{
long num3 = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
num3 &= (long)0xffffffffL;
rndSeed = (int)((num3 + (num3 >> 0x18)) & 0xffffffL);
}
rndSeed = (int)(((rndSeed * 0x43fd43fdL) + 0xc39ec3L) & 0xffffffL);
}
m_rndSeed = rndSeed;
return (((float)rndSeed) / 1.677722E+07f);
}
}