github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/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 "github.com/shogo82148/std/fmt" 9 "github.com/shogo82148/std/math/rand" 10 "github.com/shogo82148/std/os" 11 "github.com/shogo82148/std/strings" 12 "github.com/shogo82148/std/text/tabwriter" 13 ) 14 15 func Example() { 16 answers := []string{ 17 "It is certain", 18 "It is decidedly so", 19 "Without a doubt", 20 "Yes definitely", 21 "You may rely on it", 22 "As I see it yes", 23 "Most likely", 24 "Outlook good", 25 "Yes", 26 "Signs point to yes", 27 "Reply hazy try again", 28 "Ask again later", 29 "Better not tell you now", 30 "Cannot predict now", 31 "Concentrate and ask again", 32 "Don't count on it", 33 "My reply is no", 34 "My sources say no", 35 "Outlook not so good", 36 "Very doubtful", 37 } 38 fmt.Println("Magic 8-Ball says:", answers[rand.Intn(len(answers))]) 39 } 40 41 // この例では、*Randの各メソッドの使用を示しています。 42 // グローバル関数の使用は、レシーバーなしで同じです。 43 func Example_rand() { 44 // ジェネレータを作成し、シードを設定します。 45 // 通常、固定されていないシードを使用するべきで、例えばtime.Now().UnixNano()などです。 46 // 固定されたシードを使用すると、毎回の実行で同じ出力が生成されます。 47 r := rand.New(rand.NewSource(99)) 48 49 // ここでのtabwriterは、整列した出力を生成するのに役立ちます。 50 w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) 51 defer w.Flush() 52 show := func(name string, v1, v2, v3 any) { 53 fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3) 54 } 55 56 // Float32とFloat64の値は[0, 1)の範囲内です。 57 show("Float32", r.Float32(), r.Float32(), r.Float32()) 58 show("Float64", r.Float64(), r.Float64(), r.Float64()) 59 60 // ExpFloat64の値は平均が1ですが、指数関数的に減衰します。 61 show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64()) 62 63 // NormFloat64の値は平均が0で、標準偏差が1です。 64 show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64()) 65 66 // Int31、Int63、およびUint32は、指定された幅の値を生成します。 67 // Intメソッド(ここでは示されていません)は、'int'のサイズに応じてInt31またはInt63のどちらかと同様です。 68 show("Int31", r.Int31(), r.Int31(), r.Int31()) 69 show("Int63", r.Int63(), r.Int63(), r.Int63()) 70 show("Uint32", r.Uint32(), r.Uint32(), r.Uint32()) 71 72 // Intn、Int31n、およびInt63nは、出力をn未満に制限します。 73 // これらは、r.Int()%nを使用するよりも慎重に行います。 74 show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10)) 75 show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10)) 76 show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10)) 77 78 // Permは、数値[0, n)のランダムな順列を生成します。 79 show("Perm", r.Perm(5), r.Perm(5), r.Perm(5)) 80 // Output: 81 // Float32 0.2635776 0.6358173 0.6718283 82 // Float64 0.628605430454327 0.4504798828572669 0.9562755949377957 83 // ExpFloat64 0.3362240648200941 1.4256072328483647 0.24354758816173044 84 // NormFloat64 0.17233959114940064 1.577014951434847 0.04259129641113857 85 // Int31 1501292890 1486668269 182840835 86 // Int63 3546343826724305832 5724354148158589552 5239846799706671610 87 // Uint32 2760229429 296659907 1922395059 88 // Intn(10) 1 2 5 89 // Int31n(10) 4 7 8 90 // Int63n(10) 7 6 3 91 // Perm [1 4 2 3 0] [4 2 1 3 0] [1 2 4 0 3] 92 } 93 94 func ExamplePerm() { 95 for _, value := range rand.Perm(3) { 96 fmt.Println(value) 97 } 98 99 // Unordered output: 1 100 // 2 101 // 0 102 } 103 104 func ExampleShuffle() { 105 words := strings.Fields("ink runs from the corners of my mouth") 106 rand.Shuffle(len(words), func(i, j int) { 107 words[i], words[j] = words[j], words[i] 108 }) 109 fmt.Println(words) 110 } 111 112 func ExampleShuffle_slicesInUnison() { 113 numbers := []byte("12345") 114 letters := []byte("ABCDE") 115 // 数字をシャッフルし、同時にlettersの対応するエントリを交換します。 116 rand.Shuffle(len(numbers), func(i, j int) { 117 numbers[i], numbers[j] = numbers[j], numbers[i] 118 letters[i], letters[j] = letters[j], letters[i] 119 }) 120 for i := range numbers { 121 fmt.Printf("%c: %c\n", letters[i], numbers[i]) 122 } 123 } 124 125 func ExampleIntn() { 126 fmt.Println(rand.Intn(100)) 127 fmt.Println(rand.Intn(100)) 128 fmt.Println(rand.Intn(100)) 129 }