0

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

1
  • What size? What should the random components be like? Commented Dec 12, 2010 at 20:44

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.