I am trying to implement the Lagged Fibonacci generator but using a discarding strategy. I have these guidelines to follow:
Step 1: Return random numbers from the next 127 numbers generated from your RNG
Step 2: Discard the next 997 numbers generated from your RNG
Step 3: Goto Step 1.
The problem is sometimes the values generated are bopping up an error with regards to array out of bounds in section 'int randomNumber = array[Next(127)];'
The initial values are:
Random rand = new Random();
int[] array = new int[6000];
int j = 83;
static int k = 258;
int m = 2;
int n = k;
double randomNumber = 0;
Here is the code for the Discarding Method:
public int NextUsingDiscarding(int maxValue)
{
try
{
int[] array = new int[127];
for (int i = 0; i < array.Length - 1; i++)
{
array[i] = Next(maxValue);
}
int randomNumber = array[Next(127)];
for (int i = 0; i < 127 + 997; i++)
{
Next(maxValue);
}
return randomNumber;
}
catch
{
return -1;
}
}
and the number generator:
public int Next(int maxValue)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = rand.Next(maxValue);
}
int firstElement = array[n - j];
int secondElement = array[n - k];
randomNumber = (firstElement + secondElement) % Math.Pow(m, 32);
array[n] = (int)randomNumber;
return (int)randomNumber % (maxValue + 1);
}
Aucun commentaire:
Enregistrer un commentaire