Git Product home page Git Product logo

seed-random's Introduction

seed-random

Generate random numbers with a seed, useful for reproducible tests

build status Dependency Status NPM version

Installation

$ npm install seed-random

API

var assert = require('assert');
var seed = require('../');

var trueRandomA = seed();
var trueRandomB = seed();
assert(trueRandomA() != trueRandomB());

var fakeRandomA = seed('foo');
var fakeRandomB = seed('foo');
assert(fakeRandomA() == fakeRandomB());

var fakeRandomC = seed('foo', {entropy: true});
var fakeRandomD = seed('foo', {entropy: true});
assert(fakeRandomC() != fakeRandomD());


seed('foo', {global: true});//over-ride global Math.random
var numA = Math.random();
seed('foo', {global: true});
var numB = Math.random();
assert(numA == numB);//always true

seed.resetGlobal();//reset to default Math.random

License

MIT

seed-random's People

Contributors

forbeslindesay avatar

Stargazers

Don Lour avatar ZEN♡ avatar Volodymyr Ishchenko avatar Keno Medenbach avatar Peng avatar Jason Starling avatar Valerie Enfys avatar Andy Pearson avatar 你好 avatar Eduard Kyvenko avatar Joohun, Maeng avatar Cristtÿ Constantin avatar Tonio Hubilla avatar Saad Shahd avatar Maxime Aubaret avatar Guido Schmidt avatar Thomas Sileghem avatar Alexander Carusillo avatar Bradley Xu avatar Kjetil Midtgarden Golid avatar Luke Chinworth avatar Abbey Hawk Sparrow avatar Jonas Mendes avatar danielsdesk avatar Mauve Signweaver avatar petrik coffy avatar redim avatar Hasaki avatar Michael Scott Hertzberg avatar Kilian Ciuffolo avatar Julian Gruber avatar Kevin Chapelier avatar Athan avatar Dylon Edwards avatar William Casarin avatar  avatar Michael Paulukonis avatar Jason Arora avatar Sebastien Ballesteros avatar

Watchers

James Cloos avatar  avatar Michael Paulukonis avatar  avatar  avatar

seed-random's Issues

Upgrade to version 2.2

From: http://davidbau.com/encode/seedrandom.js

// seedrandom.js version 2.2.
// Author: David Bau
// Date: 2013 Jun 15

// Version notes:
//
// The random number sequence is the same as version 1.0 for string seeds.
// Version 2.0 changed the sequence for non-string seeds.
// Version 2.1 speeds seeding and uses window.crypto to autoseed if present.
// Version 2.2 alters non-crypto autoseeding to sweep up entropy from plugins.

Do you mind not generate the auto seed from the whole global object?

Hi. Thanks for the nice tool. I get here from the mathjs.

Right now, if there's no crypto on global, we will use this as the default seed: [+new Date, GLOBAL, GLOBAL.navigator && GLOBAL.navigator.plugins, GLOBAL.screen, tostring(pool)];. And then do flatten to it and convert it to a string.

But, actually, there could be some problems with this since we don't know what is existed in the user's global environment. For example, if the global environment has some Symbol values or nested Symbol values in-depth 3, then it will break by TypeError: Cannot convert a Symbol value to a string.

It's just one case that may crash the program. And there may be some others. So, I think maybe we should not use the whole user global object and do operators to it. It's a little bit out of control.

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Better cryptographic security for Node.js

Currently, the autoseed() function returns the following as randomly-generated seed:

    return [+new Date, GLOBAL, GLOBAL.navigator && GLOBAL.navigator.plugins,
            GLOBAL.screen, tostring(pool)];

This is nowhere near secure, probably even Math.random() is better than that when entropy is not enabled.

Node.js provides crypto.randomBytes() which is supposed to be "cryptographically strong pseudo-random." So why not add that as an option?

Infinite loop when generating local entropy

I stumbled upon an issue where calling seed() causes an (almost) infinite recursive loop:

  • seed() is called without arguments, in which case it uses a local entropy. This entropy is generated (amongst others) from all globally defined objects.
  • seed() is called via a getter on an object which is globally defined (obj.random in the example).
  • hence, when creating the local entropy, seed calls the obj.random getter which calls seed again -> infinite loop

node.js example code:

var seed = require('seed-random');

var count = 0;

// create an object
var obj = {};

// expose the object globally
global.obj = obj;

Object.defineProperty(obj, 'random', {
  get: function () {
    console.log('obj.random getter');
    count++;

    // call seed inside a getter of the globally defined object
    return seed();
  },
  configurable: true,
  enumerable: true
});

// call the getter... will cause an (almost) infinite loop
var a = obj.random();
console.log('a', a);

console.log('number of recursive loops over obj.random (should be 1):', count); 
// outputs for example count = 1425

Some ideas for a solution:

  • Ignore properties which are getters when creating local entropy
  • Output an informative warning when this loop happens (check whether seed is called whilst it's being executed)
  • Throw an error when seed is called whilst it is being executed, and when generating local entropy, catch this error and silently ignore it.

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.