Skip to content

Commit a79c3eb

Browse files
committed
fix: random not correctly filling rectangular matrices
1 parent 25d8853 commit a79c3eb

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/matrix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class Matrix extends Array {
131131
if (rng === undefined) rng = Math.random;
132132
var matrix = Matrix.empty(rows, columns);
133133
for (var i = 0; i < rows; i++) {
134-
for (var j = 0; j < rows; j++) {
134+
for (var j = 0; j < columns; j++) {
135135
matrix[i][j] = rng();
136136
}
137137
}

test/matrix/creation.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ describe('Matrix creation', function () {
8484
Matrix.ones(2, 3).to2DArray().should.eql([[1, 1, 1], [1, 1, 1]]);
8585
});
8686
it('random', function () {
87-
var random = Matrix.rand(2, 2);
88-
var random2 = Matrix.rand(2, 2);
87+
var random = Matrix.rand(2, 3);
88+
var random2 = Matrix.rand(2, 3);
8989
random.to2DArray().should.not.eql(random2.to2DArray());
90-
random[0][0].should.be.within(0, 1);
90+
for (var i = 0; i < 2; i++) {
91+
for (var j = 0; j < 3; j++) {
92+
random[i][j].should.be.within(0, 1);
93+
}
94+
}
9195
});
9296
it('random with custom RNG', function () {
9397
function fakeRNG() { return 2; }

0 commit comments

Comments
 (0)