나는이 클래스의 선형 방정식이상한 행동은
public class MatrixGenerator: IMatrixGenerator
{
private int vSize;
private int hSize;
private double[,] _matrix;
private double[] _right;
private double[] _solution;
private double maxValue;
public MatrixGenerator(int vSize, int hSize, double maxValue)
{
this.vSize = vSize;
this.hSize = hSize;
this.maxValue = maxValue;
_matrix = new double[vSize, hSize];
_right = new double[vSize];
_solution = new double[hSize];
}
public void Next()
{
_matrix = new double[vSize, hSize];
_right = new double[vSize];
Random r = new Random();
_solution = Enumerable.Repeat(0.0, hSize).Select(m => m = r.NextDouble()*maxValue).ToArray();
for (int i = 0; i < vSize; i++)
{
for (int j = 0; j < hSize; j++)
{
_matrix[i, j] = r.NextDouble() * maxValue;
}
for (int j = 0; j < hSize; j++)
{
_right[i] += _solution[j] * _matrix[i, j];
}
}
}
public double[,] Matrix
{
get { return _matrix; }
}
public double[] RightVector
{
get { return _right; }
}
public double[] SolutionVector
{
get { return _solution; }
}
}
와 NUnit과 테스트의 임의의 시스템을 생성 클래스가 있습니다
[Test]
public void CanGenerateAnotherMatrixandVector()
{
MatrixGenerator mGen = new MatrixGenerator(vSize, hSize, maxValue);
mGen.Next();
double[,] firstMatrix = new double[mGen.Matrix.GetLength(0), mGen.Matrix.GetLength(1)];
double[] firstVector = new double[mGen.RightVector.GetLength(0)];
for (int i = 0; i < mGen.Matrix.GetLength(0); i++)
{
firstVector[i] = mGen.RightVector[i];
for (int j = 0; j < mGen.Matrix.GetLength(1); j++)
{
firstMatrix[i,j] = mGen.Matrix[i, j];
}
}
mGen.Next();
Assert.That(firstMatrix, Is.Not.EqualTo(mGen.Matrix));
Assert.That(firstVector, Is.Not.EqualTo(mGen.RightVector));
}
테스트 실패를하지만,이 코드는 작품입니다. TestDriven.Net에서 디버거 도구로이 테스트를 디버그 해보고 모든 것이 작동하고 테스트가 통과되었습니다. 아무도 왜이 테스트가 실패하는지 설명 할 수 있습니까?
당신은 아마 그것의 경우 각 시간이 그럴듯 같은 임의의 숫자가 발생할 수 있습니다 인스턴스로'Random' 개체의 인스턴스를 재사용한다 충분히 빠른 처리. –
@ChrisSinclair에 따르면'Random r = new Random(); '을 호출하면 충분히 빨리 동일한 시퀀스를 얻을 수 있습니다. 'r'을 한 번만 만듭니다. http://stackoverflow.com/questions/767999/random-number-generator-not-working-the-way-i-had-planned-c –
마지막으로 전화하기 전에 지연을 사용하면 적절하다고 생각합니다. 다음() 메서드는이 테스트를 통과하면 – HaMI