To generate a random number in Go, you can use the math/rand
package.
Here's an example of how to use it to generate a random integer between 0 and 100:
main.go277 chars18 lines
First, we need to seed the random number generator using the current time. This ensures that each time we run the program, we get a different sequence of random numbers.
Then we generate a random integer using the rand.Intn()
function. This function returns a random integer between 0 and the given value (exclusive). In this case, we pass in 101 to generate a random integer between 0 and 100.
Finally, we print out the generated number using fmt.Println()
.
gistlibby LogSnag