import ( "fmt" "math/rand" ) func main() { n := int64(1e6) rand.Seed(time.Now().UnixNano()) num := rand.Int63n(n) fmt.Println(num) }
import ( "fmt" "math/rand" "time" ) func main() { names := []string{"Alice", "Bob", "Charlie", "Dave"} rand.Seed(time.Now().UnixNano()) index := rand.Int63n(int64(len(names))) fmt.Println(names[index]) }In both examples, we need to call `rand.Seed` to initialize the random number generator with a unique seed value. This ensures that we get different random numbers each time we run the program. The range of the generated numbers is determined by the parameter passed to `Int63n`. In Example 1, we set this to `1e6` to get a number between 0 and 10^6. In Example 2, we set it to the length of the `names` slice to get a random index to access a random name.