Skip to content

Commit e5a8541

Browse files
committed
feat: add maxValue option to Matrix.randInt
1 parent f52e4fd commit e5a8541

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/abstractMatrix.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function abstractMatrix(superCtor) {
108108
* Creates a matrix with the given dimensions. Values will be randomly set.
109109
* @param {number} rows - Number of rows
110110
* @param {number} columns - Number of columns
111-
* @param {function} [rng] - Random number generator (default: Math.random)
111+
* @param {function} [rng=Math.random] - Random number generator
112112
* @returns {Matrix} The new matrix
113113
*/
114114
static rand(rows, columns, rng) {
@@ -123,18 +123,20 @@ function abstractMatrix(superCtor) {
123123
}
124124

125125
/**
126-
* Creates a matrix with the given dimensions. Values will be randomly set.
126+
* Creates a matrix with the given dimensions. Values will be random integers.
127127
* @param {number} rows - Number of rows
128128
* @param {number} columns - Number of columns
129-
* @param {function} [rng] - Random number generator (default: Math.random)
129+
* @param {number} [maxValue=1000] - Maximum value
130+
* @param {function} [rng=Math.random] - Random number generator
130131
* @returns {Matrix} The new matrix
131132
*/
132-
static randInt(rows, columns, rng) {
133+
static randInt(rows, columns, maxValue, rng) {
134+
if (maxValue === undefined) maxValue = 1000;
133135
if (rng === undefined) rng = Math.random;
134136
var matrix = this.empty(rows, columns);
135137
for (var i = 0; i < rows; i++) {
136138
for (var j = 0; j < columns; j++) {
137-
var value = parseInt(rng()*1000);
139+
var value = Math.floor(rng() * maxValue);
138140
matrix.set(i, j, value);
139141
}
140142
}
@@ -1740,4 +1742,4 @@ function abstractMatrix(superCtor) {
17401742
}
17411743

17421744
return Matrix;
1743-
}
1745+
}

0 commit comments

Comments
 (0)