How to fill an array with 1 and 0 in random locations with Scala

1 Answer

0 votes
import scala.util.Random

object Main extends App {
  def fillArrayWithRandom1and0(array: Array[Int]): Unit = {
    for (i <- array.indices) {
      array(i) = Random.nextInt(2) // Generates either 0 or 1
    }
  }

  val size = 10
  val array = new Array[Int](size)

  fillArrayWithRandom1and0(array)

  println(array.mkString(" "))
}

  
  
/*
run:
    
1 1 0 0 0 0 1 0 1 0

*/

 



answered Jan 26, 2025 by avibootz

Related questions

...