File tree Expand file tree Collapse file tree 1 file changed +5
-9
lines changed Expand file tree Collapse file tree 1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change 11# Not So Random
22
3- Go's ` rand ` package makes it easy to generate all sorts of pseudo-random numbers.
4-
5- What they don't tell you though is that the default seed is ` 1 ` . They [ do
6- tell you that the numbers are pseudo-random and that you need to use the
7- ` Seed ` function to initialize the default source if different behavior is
8- required for each run] ( https://golang.org/pkg/math/rand/ ) , though. So if you
9- write a program like so:
3+ Go's ` rand ` package makes it easy to generate all sorts of pseudo-random
4+ numbers. So if you write a program like so:
105
116``` go
127package main
@@ -31,8 +26,9 @@ and then run it, you will get output like:
3126three
3227```
3328
34- and any subsequent runs of the program will continue to produce ` three ` . Not
35- exactly what we are looking for.
29+ and any subsequent runs of the program will continue to produce ` three ` . This
30+ is because the default seed for global functions in ` math/rand ` is
31+ [ specified] ( https://golang.org/pkg/math/rand/#Seed ) as ` 1 ` .
3632
3733If you want your program to be a little less predictable, you will want to
3834seed it yourself, perhaps with the current time, instead of ` 1 ` . Try adding
You can’t perform that action at this time.
0 commit comments