Skip to content

moul/srand

srand

πŸ˜„ Initialize random seed in Golang.

CircleCI GoDoc License GitHub release Go Report Card CodeFactor codecov GolangCI Sourcegraph Made by Manfred Touron

Usage

import (
    "math/rand"
    "moul.io/srand"
)

func init() {
    rand.Seed(srand.MustSecure())
}

Alternative seeds

// simple seed initializer
rand.Seed(srand.Fast())

// thread-safe simple seed initializer
go func() { rng := rand.New(rand.NewSource(srand.SafeFast())); fmt.Println(rng.Intn(42)) }()
go func() { rng := rand.New(rand.NewSource(srand.SafeFast())); fmt.Println(rng.Intn(42)) }()

// simple seed initializer overridable by the $SRAND env var
rand.Seed(srand.Overridable("SRAND"))

// cryptographically secure initializer
rand.Seed(srand.MustSecure())

Install

$ go get -u moul.io/srand

License

Β© 2019-2021 Manfred Touron

Licensed under the Apache License, Version 2.0 (LICENSE-APACHE) or the MIT license (LICENSE-MIT), at your option. See the COPYRIGHT file for more details.

SPDX-License-Identifier: (Apache-2.0 OR MIT)