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
5package rand_test
6
7import (
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
18func Example() {
19	answers := []string{
20		"It is certain",
21		"It is decidedly so",
22		"Without a doubt",
23		"Yes definitely",
24		"You may rely on it",
25		"As I see it yes",
26		"Most likely",
27		"Outlook good",
28		"Yes",
29		"Signs point to yes",
30		"Reply hazy try again",
31		"Ask again later",
32		"Better not tell you now",
33		"Cannot predict now",
34		"Concentrate and ask again",
35		"Don't count on it",
36		"My reply is no",
37		"My sources say no",
38		"Outlook not so good",
39		"Very doubtful",
40	}
41	fmt.Println("Magic 8-Ball says:", answers[rand.Intn(len(answers))])
42}
43
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.
46func Example_rand() {
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))
51
52	// The tabwriter here helps us generate aligned output.
53	w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
54	defer w.Flush()
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)
57	}
58
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())
62
63	// ExpFloat64 values have an average of 1 but decay exponentially.
64	show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())
65
66	// NormFloat64 values have an average of 0 and a standard deviation of 1.
67	show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())
68
69	// Int31, Int63, and Uint32 generate values of the given width.
70	// The Int method (not shown) is like either Int31 or Int63
71	// depending on the size of 'int'.
72	show("Int31", r.Int31(), r.Int31(), r.Int31())
73	show("Int63", r.Int63(), r.Int63(), r.Int63())
74	show("Uint32", r.Uint32(), r.Uint32(), r.Uint32())
75
76	// Intn, Int31n, and Int63n 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("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10))
80	show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10))
81
82	// Perm generates a random permutation of the numbers [0, n).
83	show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))
84	// Output:
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	// Int31       1501292890          1486668269          182840835
90	// Int63       3546343826724305832 5724354148158589552 5239846799706671610
91	// Uint32      2760229429          296659907           1922395059
92	// Intn(10)    1                   2                   5
93	// Int31n(10)  4                   7                   8
94	// Int63n(10)  7                   6                   3
95	// Perm        [1 4 2 3 0]         [4 2 1 3 0]         [1 2 4 0 3]
96}
97
98func ExamplePerm() {
99	for _, value := range rand.Perm(3) {
100		fmt.Println(value)
101	}
102
103	// Unordered output: 1
104	// 2
105	// 0
106}
107
108func 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]
112	})
113	fmt.Println(words)
114}
115
116func 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]
123	})
124	for i := range numbers {
125		fmt.Printf("%c: %c\n", letters[i], numbers[i])
126	}
127}
128
129func ExampleIntn() {
130	fmt.Println(rand.Intn(100))
131	fmt.Println(rand.Intn(100))
132	fmt.Println(rand.Intn(100))
133}
134