From a7a1261f52112a3bca375dd0bed1c1bc0a2e0ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B3=A2?= <601450868@qq.com> Date: Sat, 30 Jul 2016 23:43:36 +0800 Subject: [PATCH 1/2] Add rand(numRows: Int, numCols: Int) functions add rand(numRows: Int, numCols: Int) functions to DenseMatrix object,like breeze.linalg.DenseMatrix.rand() --- .../org/apache/spark/mllib/linalg/Matrices.scala | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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..05f0ccaf94f8d 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,20 @@ 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") + new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)((new Random).nextDouble())) + } + /** * Generate a `DenseMatrix` consisting of `i.i.d.` gaussian random numbers. * @param numRows number of rows of the matrix From 054b70ccce73c02cce04caf9f7958cfc555df829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B3=A2?= <601450868@qq.com> Date: Sun, 31 Jul 2016 00:36:30 +0800 Subject: [PATCH 2/2] fix RNG fix RNG , his makes a new RNG for All element --- .../main/scala/org/apache/spark/mllib/linalg/Matrices.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 05f0ccaf94f8d..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 @@ -507,7 +507,8 @@ object DenseMatrix { def rand(numRows: Int, numCols: Int): DenseMatrix = { require(numRows.toLong * numCols <= Int.MaxValue, s"$numRows x $numCols dense matrix is too large to allocate") - new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)((new Random).nextDouble())) + val rng=new Random() + new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(rng.nextDouble())) } /**