如何在scala中给出2-D数组的随机值
答案 0 :(得分:2)
Array伴随对象上的fill方法使这非常简单。
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))