golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/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  	"os"
    10  	"strings"
    11  	"text/tabwriter"
    12  
    13  	"golang.org/x/exp/rand"
    14  )
    15  
    16  // These tests serve as an example but also make sure we don't change
    17  // the output of the random number generator when given a fixed seed.
    18  
    19  func Example() {
    20  	rand.Seed(42) // Try changing this number!
    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  	// Output: Magic 8-Ball says: Most likely
    45  }
    46  
    47  // This example shows the use of each of the methods on a *Rand.
    48  // The use of the global functions is the same, without the receiver.
    49  func Example_rand() {
    50  	// Create and seed the generator.
    51  	// Typically a non-fixed seed should be used, such as time.Now().UnixNano().
    52  	// Using a fixed seed will produce the same output on every run.
    53  	r := rand.New(rand.NewSource(1234))
    54  
    55  	// The tabwriter here helps us generate aligned output.
    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  	// Float32 and Float64 values are in [0, 1).
    63  	show("Float32", r.Float32(), r.Float32(), r.Float32())
    64  	show("Float64", r.Float64(), r.Float64(), r.Float64())
    65  
    66  	// ExpFloat64 values have an average of 1 but decay exponentially.
    67  	show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())
    68  
    69  	// NormFloat64 values have an average of 0 and a standard deviation of 1.
    70  	show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())
    71  
    72  	// Int31, Int63, and Uint32 generate values of the given width.
    73  	// The Int method (not shown) is like either Int31 or Int63
    74  	// depending on the size of 'int'.
    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  	// Intn, Int31n, Int63n and Uint64n limit their output to be < n.
    81  	// They do so more carefully than using r.Int()%n.
    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  	// 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.030719291          0.47512934           0.031019364
    91  	// Float64     0.6906635660087743   0.9898818576905045   0.2683634639782333
    92  	// ExpFloat64  1.24979080914592     0.3451975160045876   0.5456817760595064
    93  	// NormFloat64 0.879221333732727    -0.01508980368383761 -1.962250558270421
    94  	// Int31       2043816560           1870670250           1334960143
    95  	// Int63       7860766611810691572  1466711535823962239  3836585920276818709
    96  	// Uint32      2051241581           751073909            1353986074
    97  	// Uint64      10802154207635843641 14398820303406316826 11052107950969057042
    98  	// Intn(10)    3                    0                    1
    99  	// Int31n(10)  3                    8                    1
   100  	// Int63n(10)  4                    6                    0
   101  	// Uint64n(10) 2                    9                    4
   102  	// Perm        [1 3 4 0 2]          [2 4 0 3 1]          [3 2 0 4 1]
   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  	// Output:
   113  	// [ink corners of from mouth runs the my]
   114  }
   115  
   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]
   123  	})
   124  	for i := range numbers {
   125  		fmt.Printf("%c: %c\n", letters[i], numbers[i])
   126  	}
   127  
   128  	// Output:
   129  	// D: 4
   130  	// A: 1
   131  	// E: 5
   132  	// B: 2
   133  	// C: 3
   134  }
   135  
   136  func ExampleLockedSource() {
   137  	r := rand.New(new(rand.LockedSource))
   138  	r.Seed(42) // Try changing this number!
   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  	// Output: Magic 8-Ball says: Most likely
   163  }