srand

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2020 License: Apache-2.0, MIT Imports: 5 Imported by: 22

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"
import "moul.io/srand"

func init() {
    // cryptographically secure initializer
    rand.Seed(srand.Secure())

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

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

    // cryptographically secure initializer
    rand.Seed(srand.Secure())
}

Install

$ go get -u moul.io/srand

License

© 2019 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 Overridable

func Overridable(key string) int64

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.

Example
package main

import (
	"math/rand"
	"os"

	"moul.io/srand"
)

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

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

func Secure

func Secure() int64

Secure returns a cryptographically secure random seed.

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

Example
package main

import (
	"math/rand"

	"moul.io/srand"
)

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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