开发者

how to give random values of 2-D array in scala?

开发者 https://www.devze.com 2023-01-30 00:03 出处:网络
how to giv开发者_运维知识库e random values of 2-D array in scalaThe fill method on the Array companion object makes this pretty simple.

how to giv开发者_运维知识库e random values of 2-D array in scala


The fill method on the Array companion object makes this pretty simple.

scala> val random = new java.security.SecureRandom                                                                                       
random: java.security.SecureRandom = java.security.SecureRandom@12cb94b7

scala> def random2dArray(dim1: Int, dim2: Int, maxValue: Int) = Array.fill(dim1, dim2) { random.nextInt(maxValue) }
random2dArray: (dim1: Int,dim2: Int,maxValue: Int)Array[Array[Int]]

scala> random2dArray(3, 3, 100)
res0: Array[Array[Int]] = Array(Array(20, 80, 12), Array(99, 63, 82), Array(9, 76, 85))
0

精彩评论

暂无评论...
验证码 换一张
取 消