Go’s |
|
![]() ![]() package main |
|
import ( "fmt" "math/rand" "time" ) |
|
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() |
The default number generator is deterministic, so it’ll
produce the same sequence of numbers each time by default.
To produce varying sequences, give it a seed that changes.
Note that this is not safe to use for random numbers you
intend to be secret; use |
s1 := rand.NewSource(time.Now().UnixNano()) 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() s3 := rand.NewSource(42) r3 := rand.New(s3) fmt.Print(r3.Intn(100), ",") fmt.Print(r3.Intn(100)) } |
Depending on where you run this sample, some of the
generated numbers may be different. Note that on
the Go playground seeding with |
$ go run random-numbers.go 81,87 0.6645600532184904 7.123187485356329,8.434115364335547 0,28 5,87 5,87 |
See the |
Next example: Number Parsing.