...

Source file src/go.mongodb.org/mongo-driver/internal/rand/example_test.go

Documentation: go.mongodb.org/mongo-driver/internal/rand

     1  // Copied from https://cs.opensource.google/go/x/exp/+/24438e51023af3bfc1db8aed43c1342817e8cfcd:rand/example_test.go
     2  
     3  // Copyright 2012 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package rand_test
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  	"text/tabwriter"
    14  
    15  	"go.mongodb.org/mongo-driver/internal/rand"
    16  )
    17  
    18  // These tests serve as an example but also make sure we don't change
    19  // the output of the random number generator when given a fixed seed.
    20  
    21  func Example() {
    22  	rand.Seed(42) // Try changing this number!
    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: Most likely
    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(1234))
    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  	show("Uint64", r.Uint64(), r.Uint64(), r.Uint64())
    81  
    82  	// Intn, Int31n, Int63n and Uint64n limit their output to be < n.
    83  	// They do so more carefully than using r.Int()%n.
    84  	show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10))
    85  	show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10))
    86  	show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10))
    87  	show("Uint64n(10)", r.Uint64n(10), r.Uint64n(10), r.Uint64n(10))
    88  
    89  	// Perm generates a random permutation of the numbers [0, n).
    90  	show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))
    91  	// Output:
    92  	// Float32     0.030719291          0.47512934           0.031019364
    93  	// Float64     0.6906635660087743   0.9898818576905045   0.2683634639782333
    94  	// ExpFloat64  1.24979080914592     0.3451975160045876   0.5456817760595064
    95  	// NormFloat64 0.879221333732727    -0.01508980368383761 -1.962250558270421
    96  	// Int31       2043816560           1870670250           1334960143
    97  	// Int63       7860766611810691572  1466711535823962239  3836585920276818709
    98  	// Uint32      2051241581           751073909            1353986074
    99  	// Uint64      10802154207635843641 14398820303406316826 11052107950969057042
   100  	// Intn(10)    3                    0                    1
   101  	// Int31n(10)  3                    8                    1
   102  	// Int63n(10)  4                    6                    0
   103  	// Uint64n(10) 2                    9                    4
   104  	// Perm        [1 3 4 0 2]          [2 4 0 3 1]          [3 2 0 4 1]
   105  }
   106  
   107  func ExampleShuffle() {
   108  	words := strings.Fields("ink runs from the corners of my mouth")
   109  	rand.Shuffle(len(words), func(i, j int) {
   110  		words[i], words[j] = words[j], words[i]
   111  	})
   112  	fmt.Println(words)
   113  
   114  	// Output:
   115  	// [ink corners of from mouth runs the my]
   116  }
   117  
   118  func ExampleShuffle_slicesInUnison() {
   119  	numbers := []byte("12345")
   120  	letters := []byte("ABCDE")
   121  	// Shuffle numbers, swapping corresponding entries in letters at the same time.
   122  	rand.Shuffle(len(numbers), func(i, j int) {
   123  		numbers[i], numbers[j] = numbers[j], numbers[i]
   124  		letters[i], letters[j] = letters[j], letters[i]
   125  	})
   126  	for i := range numbers {
   127  		fmt.Printf("%c: %c\n", letters[i], numbers[i])
   128  	}
   129  
   130  	// Output:
   131  	// D: 4
   132  	// A: 1
   133  	// E: 5
   134  	// B: 2
   135  	// C: 3
   136  }
   137  
   138  func ExampleLockedSource() {
   139  	r := rand.New(new(rand.LockedSource))
   140  	r.Seed(42) // Try changing this number!
   141  	answers := []string{
   142  		"It is certain",
   143  		"It is decidedly so",
   144  		"Without a doubt",
   145  		"Yes definitely",
   146  		"You may rely on it",
   147  		"As I see it yes",
   148  		"Most likely",
   149  		"Outlook good",
   150  		"Yes",
   151  		"Signs point to yes",
   152  		"Reply hazy try again",
   153  		"Ask again later",
   154  		"Better not tell you now",
   155  		"Cannot predict now",
   156  		"Concentrate and ask again",
   157  		"Don't count on it",
   158  		"My reply is no",
   159  		"My sources say no",
   160  		"Outlook not so good",
   161  		"Very doubtful",
   162  	}
   163  	fmt.Println("Magic 8-Ball says:", answers[r.Intn(len(answers))])
   164  	// Output: Magic 8-Ball says: Most likely
   165  }
   166  

View as plain text