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