• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D13-Mar-2017266

LICENSEH A D13-Mar-20172.6 KiB

README.mdH A D13-Mar-20171.5 KiB

init.goH A D13-Mar-20171.9 KiB

init_test.goH A D13-Mar-2017385

README.md

1# `seed` - Quickly Seed Go's Random Number Generator
2
3Boiler-plate to securely [seed](https://en.wikipedia.org/wiki/Random_seed) Go's
4random number generator (if possible).  This library isn't anything fancy, it's
5just a canonical way of seeding Go's random number generator. Cribbed from
6[`Nomad`](https://github.com/hashicorp/nomad/commit/f89a993ec6b91636a3384dd568898245fbc273a1)
7before it was moved into
8[`Consul`](https://github.com/hashicorp/consul/commit/d695bcaae6e31ee307c11fdf55bb0bf46ea9fcf4)
9and made into a helper function, and now further modularized to be a super
10lightweight and reusable library.
11
12Time is better than
13[Go's default seed of `1`](https://golang.org/pkg/math/rand/#Seed), but friends
14don't let friends use time as a seed to a random number generator.  Use
15`seed.MustInit()` instead.
16
17`seed.Init()` is an idempotent and reentrant call that will return an error if
18it can't seed the value the first time it is called.  `Init()` is reentrant.
19
20`seed.MustInit()` is idempotent and reentrant call that will `panic()` if it
21can't seed the value the first time it is called.  `MustInit()` is reentrant.
22
23## Usage
24
25```
26package mypackage
27
28import (
29  "github.com/sean-/seed"
30)
31
32// MustInit will panic() if it is unable to set a high-entropy random seed:
33func init() {
34  seed.MustInit()
35}
36
37// Or if you want to not panic() and can actually handle this error:
38func init() {
39  if secure, err := !seed.Init(); !secure {
40    // Handle the error
41    //panic(fmt.Sprintf("Unable to securely seed Go's RNG: %v", err))
42  }
43}
44```
45