github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/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  	"fmt"
     9  	"math/rand"
    10  	"os"
    11  	"text/tabwriter"
    12  )
    13  
    14  // These tests serve as an example but also make sure we don't change
    15  // the output of the random number generator when given a fixed seed.
    16  
    17  func Example() {
    18  	rand.Seed(42) // Try changing this number!
    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  	// Output: Magic 8-Ball says: As I see it yes
    43  }
    44  
    45  // This example shows the use of each of the methods on a *Rand.
    46  // The use of the global functions is the same, without the receiver.
    47  func Example_rand() {
    48  	// Create and seed the generator.
    49  	// Typically a non-fixed seed should be used, such as time.Now().UnixNano().
    50  	// Using a fixed seed will produce the same output on every run.
    51  	r := rand.New(rand.NewSource(99))
    52  
    53  	// The tabwriter here helps us generate aligned output.
    54  	w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
    55  	defer w.Flush()
    56  	show := func(name string, v1, v2, v3 interface{}) {
    57  		fmt.Fprintf(w, "%s\t%v\t%v\t%v\n", name, v1, v2, v3)
    58  	}
    59  
    60  	// Float32 and Float64 values are in [0, 1).
    61  	show("Float32", r.Float32(), r.Float32(), r.Float32())
    62  	show("Float64", r.Float64(), r.Float64(), r.Float64())
    63  
    64  	// ExpFloat64 values have an average of 1 but decay exponentially.
    65  	show("ExpFloat64", r.ExpFloat64(), r.ExpFloat64(), r.ExpFloat64())
    66  
    67  	// NormFloat64 values have an average of 0 and a standard deviation of 1.
    68  	show("NormFloat64", r.NormFloat64(), r.NormFloat64(), r.NormFloat64())
    69  
    70  	// Int31, Int63, and Uint32 generate values of the given width.
    71  	// The Int method (not shown) is like either Int31 or Int63
    72  	// depending on the size of 'int'.
    73  	show("Int31", r.Int31(), r.Int31(), r.Int31())
    74  	show("Int63", r.Int63(), r.Int63(), r.Int63())
    75  	show("Uint32", r.Uint32(), r.Uint32(), r.Uint32())
    76  
    77  	// Intn, Int31n, and Int63n limit their output to be < n.
    78  	// They do so more carefully than using r.Int()%n.
    79  	show("Intn(10)", r.Intn(10), r.Intn(10), r.Intn(10))
    80  	show("Int31n(10)", r.Int31n(10), r.Int31n(10), r.Int31n(10))
    81  	show("Int63n(10)", r.Int63n(10), r.Int63n(10), r.Int63n(10))
    82  
    83  	// Perm generates a random permutation of the numbers [0, n).
    84  	show("Perm", r.Perm(5), r.Perm(5), r.Perm(5))
    85  	// Output:
    86  	// Float32     0.2635776           0.6358173           0.6718283
    87  	// Float64     0.628605430454327   0.4504798828572669  0.9562755949377957
    88  	// ExpFloat64  0.3362240648200941  1.4256072328483647  0.24354758816173044
    89  	// NormFloat64 0.17233959114940064 1.577014951434847   0.04259129641113857
    90  	// Int31       1501292890          1486668269          182840835
    91  	// Int63       3546343826724305832 5724354148158589552 5239846799706671610
    92  	// Uint32      2760229429          296659907           1922395059
    93  	// Intn(10)    1                   2                   5
    94  	// Int31n(10)  4                   7                   8
    95  	// Int63n(10)  7                   6                   3
    96  	// Perm        [1 4 2 3 0]         [4 2 1 3 0]         [1 2 4 0 3]
    97  }
    98  
    99  func ExamplePerm() {
   100  	for _, value := range rand.Perm(3) {
   101  		fmt.Println(value)
   102  	}
   103  
   104  	// Unordered output: 1
   105  	// 2
   106  	// 0
   107  }