Short code with new Array creation:
val terrainMap = Array.tabulate(width, height){ (_, _) => terrainTypes(Random.nextInt(terrainTypes.length)) }
If you need to optimize the for loop, check out Scalaxy :
for { i <- 0 until width optimized; j <- 0 until height optimized } { val r = Random.nextInt(terrainTypes.length) terrainMap(i)(j) = terrainTypes(r) }
Scalaxy optimizes for-comprehensions using a while loop.
source share