Coder Social home page Coder Social logo

wk8 / go-rand Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 31 KB

Drop-in replacement for `math/rand` that allows snapshotting/restoring the internal state of random sources to easily reproduce flaky tests.

License: MIT License

Makefile 0.42% Go 99.58%

go-rand's Introduction

CircleCI

Marshallable golang random generator

Why?

This package is a drop-in replacement for math/rand, except it also allows you to serialize the random sources' internal state, and restore it at will.

That can come in handy when debugging flaky tests: if you know an initial seed for your whole suite that makes some test fail, you don't need to replay all your tests up to the one that fails any more: just dump the random generator's internal state at the beginning of the failing test once, and you can then load that state to only run the offending test.

Usage

It's a drop-in replacement for math/rand with just a couple of methods on top:

import (
"github.com/wk8/go-rand"
)

source := rand.NewSource(time.Now().UnixNano())
state, err := source.Marshall()
if err != nil {
// ...
}
r := rand.New(source)

// say the next call to r.Int() yields 12

if err := source.Unmarshall(); err != nil {
// ...
}

// your source is now re-set to what it was when it was marshalled
// in particular the next call to r.Int() will yield 12 again

or if you prefer the global version:

state, err := rand.Marshall()
// ...
err = rand.Unmarshall(state)

How do I know I can trust this repo?

It uses the exact same RNG as math/rand; in fact most of this repo is copy-pasted directly from there.

That's also why given the same seed, it will give you the exact same pseudo-random values as math/rand's generator.

And for this very same reason, you shouldn't trust this repo any more than math/rand's generator, which is not crypto-secure

go-rand's People

Contributors

wk8 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.