def rand(seed:Int):Double={
val rand=new Random(seed)
rand.nextDouble()
}
def rand2(size:Int,seed:Int):SparseVector={
val rand=new Random(seed)
val ret_indices=Array.range(0,size)
val ret_values=Array.tabulate[Double](size)(i=>rand.nextDouble()) //从函数创建数组
Vectors.sparse(size, ret_indices, ret_values).asInstanceOf[SparseVector]
}
调用:val w=rand2(4,3)
返回,SparseVector有3个要素函数,size,indices,values.
取某一部分就调用那3个函数。比如:w.size=4,w.indices=(1,2,3,4)
def rand3(numRows:Int,numCols:Int,seed:Int):SparseMatrix={
val rand=new Random(seed)
val ret_rowIndices=new ArrayBuffer[Int]()
for (i <-0 to numCols-1) {
ret_rowIndices ++= Array.range(0,numRows)
}
val ret_colPtrs=Array.range(0,numCols+1).map(x=>x * numRows)
val ret_values=Array.tabulate[Double](numRows*numCols)(i=>rand.nextDouble())
Matrices.sparse(numRows, numCols, ret_colPtrs.toArray,ret_rowIndices.toArray, ret_values.toArray).asInstanceOf[SparseMatrix]
}