1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 // These tests serve as an example but also make sure we don't change
16 // the output of the random number generator when given a fixed seed.
30 "Reply hazy try again",
32 "Better not tell you now",
34 "Concentrate and ask again",
38 "Outlook not so good",
41 fmt.Println("Magic 8-Ball says:", answers[rand.IntN(len(answers))])
44 // This example shows the use of each of the methods on a *Rand.
45 // The use of the global functions is the same, without the receiver.
47 // Create and seed the generator.
48 // Typically a non-fixed seed should be used, such as time.Now().UnixNano().
49 // Using a fixed seed will produce the same output on every run.
50 r := rand.New(rand.NewSource(99))
52 // The tabwriter here helps us generate aligned output.
53 w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
55 show := func(name string, v1, v2, v3 any) {
56 fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3)
59 // Float32 and Float64 values are in [0, 1).
60 show("Float32", r.Float32(), r.Float32(), r.Float32())
61 show("Float64", r.Float64(), r.Float64(), r.Float64())
63 // ExpFloat64 values have an average of 1 but decay exponentially.
64 show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())
66 // NormFloat64 values have an average of 0 and a standard deviation of 1.
67 show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())
69 // Int32, Int64, and Uint32 generate values of the given width.
70 // The Int method (not shown) is like either Int32 or Int64
71 // depending on the size of 'int'.
72 show("Int32", r.Int32(), r.Int32(), r.Int32())
73 show("Int64", r.Int64(), r.Int64(), r.Int64())
74 show("Uint32", r.Uint32(), r.Uint32(), r.Uint32())
76 // IntN, Int32N, and Int64N limit their output to be < n.
77 // They do so more carefully than using r.Int()%n.
78 show("IntN(10)", r.IntN(10), r.IntN(10), r.IntN(10))
79 show("Int32N(10)", r.Int32N(10), r.Int32N(10), r.Int32N(10))
80 show("Int64N(10)", r.Int64N(10), r.Int64N(10), r.Int64N(10))
82 // Perm generates a random permutation of the numbers [0, n).
83 show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))
85 // Float32 0.2635776 0.6358173 0.6718283
86 // Float64 0.628605430454327 0.4504798828572669 0.9562755949377957
87 // ExpFloat64 0.3362240648200941 1.4256072328483647 0.24354758816173044
88 // NormFloat64 0.17233959114940064 1.577014951434847 0.04259129641113857
89 // Int32 1501292890 1486668269 182840835
90 // Int64 3546343826724305832 5724354148158589552 5239846799706671610
91 // Uint32 2760229429 296659907 1922395059
95 // Perm [1 4 2 3 0] [4 2 1 3 0] [1 2 4 0 3]
99 for _, value := range rand.Perm(3) {
103 // Unordered output: 1
108 func ExampleShuffle() {
109 words := strings.Fields("ink runs from the corners of my mouth")
110 rand.Shuffle(len(words), func(i, j int) {
111 words[i], words[j] = words[j], words[i]
116 func ExampleShuffle_slicesInUnison() {
117 numbers := []byte("12345")
118 letters := []byte("ABCDE")
119 // Shuffle numbers, swapping corresponding entries in letters at the same time.
120 rand.Shuffle(len(numbers), func(i, j int) {
121 numbers[i], numbers[j] = numbers[j], numbers[i]
122 letters[i], letters[j] = letters[j], letters[i]
124 for i := range numbers {
125 fmt.Printf("%c: %c\n", letters[i], numbers[i])
130 fmt.Println(rand.IntN(100))
131 fmt.Println(rand.IntN(100))
132 fmt.Println(rand.IntN(100))