diff --git a/mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala b/mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala index e8f34388cd9fe..0f2ac8a68f9f6 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala @@ -496,6 +496,21 @@ object DenseMatrix { new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(rng.nextDouble())) } + /** + * Generate a `DenseMatrix` consisting of `i.i.d.` uniform random numbers. + * + * @param numRows number of rows of the matrix + * @param numCols number of columns of the matrix + * @return DenseMatrix` with size `numRows` x `numCols` and values in U(0, 1) + */ + @Since("2.0.0") + def rand(numRows: Int, numCols: Int): DenseMatrix = { + require(numRows.toLong * numCols <= Int.MaxValue, + s"$numRows x $numCols dense matrix is too large to allocate") + val rng=new Random() + new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(rng.nextDouble())) + } + /** * Generate a `DenseMatrix` consisting of `i.i.d.` gaussian random numbers. * @param numRows number of rows of the matrix