1
2
3
4
5
6
7 package rand_test
8
9 import (
10 "fmt"
11 "os"
12 "strings"
13 "text/tabwriter"
14
15 "go.mongodb.org/mongo-driver/internal/rand"
16 )
17
18
19
20
21 func Example() {
22 rand.Seed(42)
23 answers := []string{
24 "It is certain",
25 "It is decidedly so",
26 "Without a doubt",
27 "Yes definitely",
28 "You may rely on it",
29 "As I see it yes",
30 "Most likely",
31 "Outlook good",
32 "Yes",
33 "Signs point to yes",
34 "Reply hazy try again",
35 "Ask again later",
36 "Better not tell you now",
37 "Cannot predict now",
38 "Concentrate and ask again",
39 "Don't count on it",
40 "My reply is no",
41 "My sources say no",
42 "Outlook not so good",
43 "Very doubtful",
44 }
45 fmt.Println("Magic 8-Ball says:", answers[rand.Intn(len(answers))])
46
47 }
48
49
50
51 func Example_rand() {
52
53
54
55 r := rand.New(rand.NewSource(1234))
56
57
58 w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
59 defer w.Flush()
60 show := func(name string, v1, v2, v3 interface{}) {
61 fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3)
62 }
63
64
65 show("Float32", r.Float32(), r.Float32(), r.Float32())
66 show("Float64", r.Float64(), r.Float64(), r.Float64())
67
68
69 show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())
70
71
72 show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())
73
74
75
76
77 show("Int31", r.Int31(), r.Int31(), r.Int31())
78 show("Int63", r.Int63(), r.Int63(), r.Int63())
79 show("Uint32", r.Uint32(), r.Uint32(), r.Uint32())
80 show("Uint64", r.Uint64(), r.Uint64(), r.Uint64())
81
82
83
84 show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10))
85 show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10))
86 show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10))
87 show("Uint64n(10)", r.Uint64n(10), r.Uint64n(10), r.Uint64n(10))
88
89
90 show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 }
106
107 func ExampleShuffle() {
108 words := strings.Fields("ink runs from the corners of my mouth")
109 rand.Shuffle(len(words), func(i, j int) {
110 words[i], words[j] = words[j], words[i]
111 })
112 fmt.Println(words)
113
114
115
116 }
117
118 func ExampleShuffle_slicesInUnison() {
119 numbers := []byte("12345")
120 letters := []byte("ABCDE")
121
122 rand.Shuffle(len(numbers), func(i, j int) {
123 numbers[i], numbers[j] = numbers[j], numbers[i]
124 letters[i], letters[j] = letters[j], letters[i]
125 })
126 for i := range numbers {
127 fmt.Printf("%c: %c\n", letters[i], numbers[i])
128 }
129
130
131
132
133
134
135
136 }
137
138 func ExampleLockedSource() {
139 r := rand.New(new(rand.LockedSource))
140 r.Seed(42)
141 answers := []string{
142 "It is certain",
143 "It is decidedly so",
144 "Without a doubt",
145 "Yes definitely",
146 "You may rely on it",
147 "As I see it yes",
148 "Most likely",
149 "Outlook good",
150 "Yes",
151 "Signs point to yes",
152 "Reply hazy try again",
153 "Ask again later",
154 "Better not tell you now",
155 "Cannot predict now",
156 "Concentrate and ask again",
157 "Don't count on it",
158 "My reply is no",
159 "My sources say no",
160 "Outlook not so good",
161 "Very doubtful",
162 }
163 fmt.Println("Magic 8-Ball says:", answers[r.Intn(len(answers))])
164
165 }
166
View as plain text