Giter VIP home page Giter VIP logo

seedbank's Introduction

*** NOTE: Discontinued. *** This project is discontinued. The author now considers this to be a poor method. A superior method is to always use a fixed seed during development. See the Anne of Green Garbles gen-from-model.py script for more information.


seedbank

One of the gotchas with writing programs that use randomness is reproducibility.

Like, if you're developing a Markov-chain-based generator for NaNoGenMo, and you're running it a few times while tweaking it, and — hey, that output was pretty interesting! — but you've already pressed ctrl-C and now it's gone forever.

A good solution is to write your program so that it can take a given random seed as input somehow, and if one is not given, pick one randomly and report it somehow, for possible future use.

You could implement that pattern using a command-line option and whatnot, but that gets tedious and possibly inconsistent if you're developing multiple such programs. So seedbank aims to make it dead simple — a one-line change to your script.

Usage

First, make sure the seedbank module is on your PYTHONPATH. For example, you might add this line to your .bashrc:

export PYTHONPATH=/path/to/this/repo/src:$PYTHONPATH

Then, in your Python program, where you would normally say

import random

you instead say

import seedbank as random

and you can continue to use all the functions in the random module as normal, while seedbank takes care of the seeding and reporting in the following manner:

  • If the environment variable SEEDBANK_SEED was set,
    • If it was set to an integer, it uses that as the seed for the random number generator.
    • If it was set to LAST, it uses the last seed that was recorded in the log file, as the seed.
    • Otherwise, it picks an integer at random to use as the seed.
  • After the seed is picked, it appends a line to a logfile which contains the name of the script, the timestamp, and the chosen seed. If a file called seedbank.log exists in your home directory, that file will be used as the logfile. Otherwise, the file seedbank.log in the current directory will be used (it will be created if it doesn't exist.)

Then, if you ever want to re-run with a seed that was picked, you can review the log file, pick the seed you want, and set SEEDBANK_SEED to that. Or just set SEEDBANK_SEED=LAST to re-use the immediately previous seed.

You can of course import individual functions from seedbank as if they were from random, and that works too:

from seedbank import choice, randint

Example

The programs in bin/ in this repo demonstrate the concept. Example transcript:

$ bin/seedbank_demo1
eoaeuio
$ bin/seedbank_demo1
aoueo
$ cat seedbank.log 
bin/seedbank_demo1: 2015-10-11 10:39:11.440595: 615184
bin/seedbank_demo1: 2015-10-11 10:39:12.495845: 100141
$ SEEDBANK_SEED=615184 bin/seedbank_demo1
eoaeuio
$ 

seedbank's People

Contributors

cpressey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

tinruufu

seedbank's Issues

be on pypi

it'd be super neat if this project was available with a simple pip install seedbank

Shortcut to just re-use the last seed

Something like SEEDBANK_SEED=LAST should just look for the very last seed in seedbank.log and use that. That's a very common use case when you're debugging the deterministic parts of the program based on some output you just saw and you want to generate that output again to see if you fixed the thing you wanted to fix. Having to open the log and hunt for the last seed and copy it and paste it into the command line is a bit annoying.

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.