srand

package module
Version: v1.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2020 License: Apache-2.0, MIT Imports: 8 Imported by: 25

README

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-2020 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)

Documentation

Overview

message from the author:

+--------------------------------------------------------------+
| * * * ░░░░░░░░░░░░░░░░░░░░  Hello  ░░░░░░░░░░░░░░░░░░░░░░░░░░|
+--------------------------------------------------------------+
|                                                              |
|     ++              ______________________________________   |
|     ++++           /                                      \  |
|      ++++          |                                      |  |
|    ++++++++++      |   Feel free to contribute to this    |  |
|   +++       |      |       project or contact me on       |  |
|   ++         |     |    manfred.life if you like this     |  |
|   +  -==   ==|     |               project!               |  |
|  (   <*>   <*>     |                                      |  |
|   |          |    /|                  :)                  |  |
|   |         _)   / |                                      |  |
|   |      +++    /  \______________________________________/  |
|    \      =+   /                                             |
|     \      +                                                 |
|     |\++++++                                                 |
|     |  ++++      ||//                                        |
|  ___|   |___    _||/__                                     __|
| /    ---    \   \|  |||                   __ _  ___  __ __/ /|
|/  |       |  \    \ /                    /  ' \/ _ \/ // / / |
||  |       |  |    | |                   /_/_/_/\___/\_,_/_/  |
+--------------------------------------------------------------+

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fast

func Fast() int64

Fast returns value that can be used to initialize random seed. That value is not cryptographically secure.

Example
package main

import (
	"math/rand"

	"moul.io/srand"
)

func main() {
	rand.Seed(srand.Fast())
}
Output:

func MustOverridable added in v1.5.0

func MustOverridable(key string) int64
Example
package main

import (
	"math/rand"
	"os"

	"moul.io/srand"
)

func main() {
	rand.Seed(srand.MustOverridable("SRAND")) // seed with Fast

	os.Setenv("SRAND", "42")
	rand.Seed(srand.MustOverridable("SRAND")) // seed with 42
}
Output:

func MustSecure added in v1.5.0

func MustSecure() int64
Example
package main

import (
	"math/rand"

	"moul.io/srand"
)

func main() {
	rand.Seed(srand.MustSecure())
}
Output:

func Overridable

func Overridable(key string) (int64, error)

Overridable will check if the "key" var is configured, else it will return a Fast random seed.

If $SRAND is not parseable, panic is raised.

func SafeFast added in v1.6.0

func SafeFast() int64

SafeFast is a thread-safe version of Fast.

Example
package main

import (
	"fmt"
	"math/rand"

	"moul.io/srand"
)

func main() {
	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)) }()
	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)) }()
	go func() { rng := rand.New(rand.NewSource(srand.SafeFast())); fmt.Println(rng.Intn(42)) }()
}
Output:

func Secure

func Secure() (int64, error)

Secure returns a cryptographically secure random seed.

Based on https://stackoverflow.com/a/54491783

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL