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

1 Answer

0 votes
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func rollDice() int {
    return rand.Intn(6) + 1 // 0–5 → 1–6
}

func main() {
    rand.Seed(time.Now().UnixNano()) // seed once

    dice1 := rollDice()
    dice2 := rollDice()

    fmt.Println("Dice 1:", dice1)
    fmt.Println("Dice 2:", dice2)
}



/*
run:

Dice 1: 3
Dice 2: 1

*/

 



answered Feb 18 by avibootz

Related questions

...