I have a program to generate pseudo-random numbers by means of the mixed congruencial method, but it throws me an error in one of the code lines.
static void Main(string[] args)
{
double c, a, m, sem, res = 0, lim = 0, conta = 0, i = 0;
int j = 0, cont1 = 0, cont2 = 0;
Console.WriteLine("Ingrese constante aditiva");
c = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Ingrese Constante Multiplicativa");
a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Ingrese modulo");
m = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Ingrese semilla");
sem = Convert.ToDouble(Console.ReadLine());
double[] vector = new double[100];
double[] v = new double[4];
double[] ve = new double[5];
int[] resultado = new int[100];
//suma = num1 + num2;
while (res != 1)
{
lim = sem;
res = ((sem * a) + c) % m;
sem = res;
//conta++;
//Console.WriteLine("" + res / m);
vector[j] = res / m;
j++;
}
//Console.WriteLine("Periodo " + conta);
for (int k = 0; k < vector.Length; k++)
{
Console.WriteLine("" + vector[k]);
}
for (int l = 0; l < vector.Length; l++)
{
for (int w = 1; w < vector.Length - 1; w++)
{
if (vector[l] < vector[w])
{
resultado[l] = 0;
}
else if (vector[l] > vector[w])
{
resultado[l] = 1;
}
}
}
for (int k = 0; k < resultado.Length; k++)
{
//if(resultado[k]=resultado[k+1])
if (resultado[k] == 1)
if (resultado[k] == 1)
if (resultado[k] == 1)
if (resultado[k] == 1)
if (resultado[k] == 1)
{
cont1 = 5;
}
else cont1 = 4;
else cont1 = 3;
else cont1 = 2;
else cont1 = 1;
Console.WriteLine("Vector Resultado" + "[" + resultado[k] + "]" + cont1);
}
Console.WriteLine("Press any key to continue . . .");
Console.ReadLine();
}
}
}
The error gives me in the following line of code
vector[j] = res / m;
j++;
I get the following error:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
I hope you can help me please