Most optimized way to update elements of a 2d array in Scala

scala

Solution

If you want to update an array which already exists:

terrainMap.foreach(_.transform(_ =>
  terrainTypes(Random.nextInt(terrainTypes.length))
))

Problem

This piece of code updates all elements of a 2d array with some random value, is there any another simple and short code to solve this problem? ``` val terrainTypes = TerrainBlockType.values (0 until width).foreach(i => { (0 until height).foreach(j => { val r = Random.nextInt(terrainTypes.length) terrainMap(i)(j) = terrainTypes(r) }) }) ```

Original source