How to simulate rolling two dice (game cubes) with values 1–6 in Scala

1 Answer

0 votes
import scala.util.Random

object DiceRollProgram {

  private val rng = new Random()

  // Returns a random number from 1 to 6
  def rollDice(): Int =
    rng.nextInt(6) + 1   // 0–5 → 1–6

  def main(args: Array[String]): Unit = {
    val dice1 = rollDice()
    val dice2 = rollDice()

    println(s"Dice 1: $dice1")
    println(s"Dice 2: $dice2")
  }
}

  
  
  
/*
run:
  
Dice 1: 5
Dice 2: 4
  
*/

 

 



answered Feb 18 by avibootz

Related questions

...