-
Notifications
You must be signed in to change notification settings - Fork 3
/
program.go
48 lines (40 loc) · 967 Bytes
/
program.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
p := fmt.Println
// 0 <= i < 100
p(rand.Intn(100), ",")
p(rand.Intn(100))
p()
// 0.0 <= f < 1.0
p(rand.Float64())
// 5.0 <= f' < 10.0
p((rand.Float64()*5)+5, ",")
p((rand.Float64() * 5) + 5)
p()
// 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)
p(r1.Intn(100), ",")
p(r1.Intn(100))
p()
s2 := rand.NewSource(42)
r2 := rand.New(s2)
p(r2.Intn(100), ",")
p(r2.Intn(100))
p()
// If you seed a source with the same number, it produces the same
// sequence of random numbers.
s3 := rand.NewSource(42)
r3 := rand.New(s3)
p(r3.Intn(100), ",")
p(r3.Intn(100))
}