import ( "fmt" "math/rand" ) func main() { rand.Seed(time.Now().UnixNano()) // set seed to current time num := rand.Intn(10) // generate a random number between 0 and 9 fmt.Println("Random number:", num) }
import ( "fmt" "math/rand" ) func main() { rand.Seed(time.Now().UnixNano()) num := rand.Intn(101) - 50 // generate a random number between -50 and 50 fmt.Println("Random number:", num) }In both examples, the RandInt function is used to generate a random integer within a specified range. The first example generates a number between 0 and 9 inclusive, while the second example generates a number between -50 and 50 inclusive. The math/rand package is used in both examples.