Go’s |
|
package main
|
|
import "fmt"
import "math/rand"
|
|
func main() {
|
|
For example, |
fmt.Print(rand.Intn(100), ",")
fmt.Print(rand.Intn(100))
fmt.Println()
|
|
fmt.Println(rand.Float64())
|
This can be used to generate random floats in
other ranges, for example |
fmt.Print((rand.Float64()*5)+5, ",")
fmt.Print((rand.Float64() * 5) + 5)
fmt.Println()
|
To make the pseudorandom generator deterministic, give it a well-known seed. |
s1 := rand.NewSource(42)
r1 := rand.New(s1)
|
Call the resulting |
fmt.Print(r1.Intn(100), ",")
fmt.Print(r1.Intn(100))
fmt.Println()
|
If you seed a source with the same number, it produces the same sequence of random numbers. |
s2 := rand.NewSource(42)
r2 := rand.New(s2)
fmt.Print(r2.Intn(100), ",")
fmt.Print(r2.Intn(100))
fmt.Println()
}
|
$ go run random-numbers.go
81,87
0.6645600532184904
7.123187485356329,8.434115364335547
5,87
5,87
|
|
See the |
Previous example: Time Formatting / Parsing.
Next example: Number Parsing.