Go's `math/rand` package provides [pseudorandom number](http://en.wikipedia.org/wiki/Pseudorandom_number_generator) generation. | ||
package main |
||
import ( |
||
"fmt" |
||
"math/rand" |
||
"time" |
||
) |
||
func main() { |
||
For example, `rand.Intn` returns a random `int` n, `0 <= n < 100`. | fmt.Print(rand.Intn(100), ",") |
81, |
fmt.Print(rand.Intn(100)) |
81,87 |
|
fmt.Println() |
81,87 |
|
`rand.Float64` returns a `float64` `f`, `0.0 <= f < 1.0`. | fmt.Println(rand.Float64()) |
0.6645600532184904 |
This can be used to generate random floats in other ranges, for example `5.0 <= f' < 10.0`. | fmt.Print((rand.Float64()*5)+5, ",") |
7.1885709359349015, |
fmt.Print((rand.Float64() * 5) + 5) |
7.1885709359349015,7.123187485356329 |
|
fmt.Println() |
7.1885709359349015,7.123187485356329 |
|
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 `crypto/rand` for those. | s1 := rand.NewSource(time.Now().UnixNano()) |
|
r1 := rand.New(s1) |
||
Call the resulting `rand.Rand` just like the functions on the `rand` package. | fmt.Print(r1.Intn(100), ",") |
34, |
fmt.Print(r1.Intn(100)) |
58,96 |
|
fmt.Println() |
91,65 |
|
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), ",") |
81,72 5, |
|
fmt.Print(r2.Intn(100)) |
49,65 5,87 |
|
fmt.Println() |
9,53 5,87 |
|
s3 := rand.NewSource(42) |
||
r3 := rand.New(s3) |
||
fmt.Print(r3.Intn(100), ",") |
58,9 5, |
|
fmt.Print(r3.Intn(100)) |
14,0 5,87 |
|
} |
15,80 |