github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/math/rand/example_test.go (about) 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. 4 5 package rand_test 6 7 import ( 8 "fmt" 9 "math/rand" 10 "os" 11 "strings" 12 "text/tabwriter" 13 ) 14 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. 17 18 func Example() { 19 // Seeding with the same value results in the same random sequence each run. 20 // For different numbers, seed with a different value, such as 21 // time.Now().UnixNano(), which yields a constantly-changing number. 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 // Output: Magic 8-Ball says: As I see it yes 47 } 48 49 // This example shows the use of each of the methods on a *Rand. 50 // The use of the global functions is the same, without the receiver. 51 func Example_rand() { 52 // Create and seed the generator. 53 // Typically a non-fixed seed should be used, such as time.Now().UnixNano(). 54 // Using a fixed seed will produce the same output on every run. 55 r := rand.New(rand.NewSource(99)) 56 57 // The tabwriter here helps us generate aligned output. 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 // Float32 and Float64 values are in [0, 1). 65 show("Float32", r.Float32(), r.Float32(), r.Float32()) 66 show("Float64", r.Float64(), r.Float64(), r.Float64()) 67 68 // ExpFloat64 values have an average of 1 but decay exponentially. 69 show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64()) 70 71 // NormFloat64 values have an average of 0 and a standard deviation of 1. 72 show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64()) 73 74 // Int31, Int63, and Uint32 generate values of the given width. 75 // The Int method (not shown) is like either Int31 or Int63 76 // depending on the size of 'int'. 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 81 // Intn, Int31n, and Int63n limit their output to be < n. 82 // They do so more carefully than using r.Int()%n. 83 show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10)) 84 show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10)) 85 show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10)) 86 87 // Perm generates a random permutation of the numbers [0, n). 88 show("Perm", r.Perm(5), r.Perm(5), r.Perm(5)) 89 // Output: 90 // Float32 0.2635776 0.6358173 0.6718283 91 // Float64 0.628605430454327 0.4504798828572669 0.9562755949377957 92 // ExpFloat64 0.3362240648200941 1.4256072328483647 0.24354758816173044 93 // NormFloat64 0.17233959114940064 1.577014951434847 0.04259129641113857 94 // Int31 1501292890 1486668269 182840835 95 // Int63 3546343826724305832 5724354148158589552 5239846799706671610 96 // Uint32 2760229429 296659907 1922395059 97 // Intn(10) 1 2 5 98 // Int31n(10) 4 7 8 99 // Int63n(10) 7 6 3 100 // Perm [1 4 2 3 0] [4 2 1 3 0] [1 2 4 0 3] 101 } 102 103 func ExamplePerm() { 104 for _, value := range rand.Perm(3) { 105 fmt.Println(value) 106 } 107 108 // Unordered output: 1 109 // 2 110 // 0 111 } 112 113 func ExampleShuffle() { 114 words := strings.Fields("ink runs from the corners of my mouth") 115 rand.Shuffle(len(words), func(i, j int) { 116 words[i], words[j] = words[j], words[i] 117 }) 118 fmt.Println(words) 119 120 // Output: 121 // [mouth my the of runs corners from ink] 122 } 123 124 func ExampleShuffle_slicesInUnison() { 125 numbers := []byte("12345") 126 letters := []byte("ABCDE") 127 // Shuffle numbers, swapping corresponding entries in letters at the same time. 128 rand.Shuffle(len(numbers), func(i, j int) { 129 numbers[i], numbers[j] = numbers[j], numbers[i] 130 letters[i], letters[j] = letters[j], letters[i] 131 }) 132 for i := range numbers { 133 fmt.Printf("%c: %c\n", letters[i], numbers[i]) 134 } 135 136 // Output: 137 // C: 3 138 // D: 4 139 // A: 1 140 // E: 5 141 // B: 2 142 } 143 144 func ExampleIntn() { 145 // Seeding with the same value results in the same random sequence each run. 146 // For different numbers, seed with a different value, such as 147 // time.Now().UnixNano(), which yields a constantly-changing number. 148 rand.Seed(86) 149 fmt.Println(rand.Intn(100)) 150 fmt.Println(rand.Intn(100)) 151 fmt.Println(rand.Intn(100)) 152 153 // Output: 154 // 42 155 // 76 156 // 30 157 }