import ( "fmt" "math/rand" ) func main() { // Set the seed value to ensure different results every time rand.Seed(time.Now().UnixNano()) // Generate a random number between 0 and 100 fmt.Println(rand.Intn(100)) }
import ( "fmt" "math/rand" ) func main() { // Set the seed value to ensure different results every time rand.Seed(time.Now().UnixNano()) // Declare a slice of integers nums := []int{1, 2, 3, 4, 5} // Shuffle the slice rand.Shuffle(len(nums), func(i, j int) { nums[i], nums[j] = nums[j], nums[i] }) // Print the shuffled slice fmt.Println(nums) }
import ( "fmt" "math/rand" "time" ) func main() { // Set the seed value to ensure different results every time rand.Seed(time.Now().UnixNano()) // Define the characters to choose from chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" // Generate the password with a length of 8 password := make([]byte, 8) for i := range password { password[i] = chars[rand.Intn(len(chars))] } // Convert the byte slice to a string and print the password fmt.Println(string(password)) }The `math/rand` package is a component of the Go standard library.