GithubHelp home page GithubHelp logo

random-js's Introduction

random-js

A fairly close port of the Python Standard Library’s random module (docs, source), but using a fairly simple multiply with carry + xorshift PRNG, instead of the Mersenne Twister that Python uses by default. This PRNG should be suitable for most Monte Carlo simulations likely to run in a browser, or for purposes like procedural art. A slower but higher quality PRNG is also provided in the HighQualityRandom class. Only a few methods need be overridden to add a custom PRNG, if this still doesn’t do the trick.

Like the Python version, this module provides useful functions for dealing both with choosing integers/array items, and for generating random numbers following several common statistical distributions.

random = new require('random-py').Random

# set the PRNG’s seed, so that the precise sequence of numbers can be
# regenerated. When the class is first constructed, the PRNG is seeded
# with `+new Date`
random.seed(12345)

# choose a floating point number in the range [0, 1)
random.random()

# choose a floating point number in the range [1.5, 10)
random.uniform(1.5, 10)

# choose an integer N in the range 2 <= N < 50, by 2
random.randrange(2, 50, 2)

# or choose an integer N in the range 0 <= N < 45
random.randrange(45)

# choose an integer N in the range 5 <= N <= 18, endpoint included
random.randint(5, 18)

# randomly choose an element of an array
array = 'abcdefg'.split('')
random.choice(array)

# choose 4 elements from the array, ordered, chosen without replacement
random.sample(array, 4)

# randomly shuffle the array, in place
random.shuffle(array)

# choose a random number from the standard normal distribution
random.gauss()

# choose a random number from the normal distribution with mean 5 and
# standard deviation 5
random.gauss(5, 5)

# choose from the triangular distribution on range [10, 20) with
# mode (peak) 18
random.triangular(10, 20, 18)

# choose from the triangular distribution on range [0, 1) with mode 0.5
random.triangular()

# choose from the log normal distribution. the log of this distribution
# is the normal distibution with mean 5 and standard deviation 5
random.lognormvariate(5, 5)

# choose from the Von Mises distribution, an analog of the normal distribution
# wrapped around a circle, with mean angle π, and concentration parameter π/2
random.vonmisesvariate(Math.PI, Math.PI/2)

# other distributions:
#   - expovariate
#   - gammavariate
#   - betavariate
#   - paretovariate
#   - weibullvariate

# it’s possible to save and restore the state of the PRNG, allowing the same
# set of random numbers to be generated in the same order:
some_state = random.getstate()

a = random.random()
random.random() for i in [0...1000]

random.setstate(some_state)

random.random() == a

# * * * * * * * * * * * * *

# If the built-in PRNG doesn’t meet your needs, it is easy to
# override with your own PRNG. But this module also ships with
# a couple of alternatives.
{BaseRandom, BuiltinRandom, HighQualityRandom} = require('random-py')

# First, `BuiltinRandom` generates random numbers approximately 10
# times as fast as `Random`. It calls the built-in `Math.random`
# function twice to construct a random number between 0 and 1
# with a full 52 bits of entropy. Unfortunately, typical JavaScript
# engines have PRNGs with rather poor performance on statistical
# tests of randomness, and this class also does not support setting
# a custom seed or saving/restoring the PRNG state.
random = new BuiltinRandom
random.random()

# The other PRNG provided has a much longer period and should pass
# more rigorous statistical tests, at the cost of running roughly 8–10
# times slower:
random = new HighQualityRandom
random.random()

# It is also quite straight-forward to implement a better custom PRNG:
class XKCDRandom extends BaseRandom
    # http://xkcd.com/221/
    _randint32: -> 4
    _seed: (j) -> # ignore j
    _getstate: ->
    _setstate: ->

class DilbertRandom extends BaseRandom
    # http://dilbert.com/fast/2001-10-25/
    _randint32: -> 9
    _seed: (j) -> # ignore j
    _getstate: ->
    _setstate: ->

Random Number Tour of Accounting

random-js's People

Contributors

jrus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

random-js's Issues

Value "state" has no method 'isArray'

Thanks for making this, I need it!

It doesn't run for me. I tried compiling (coffee -c) and just plain running. Same error (below). Unfortunately I'm a total CoffeeScript/JS newb so I can't provide a helpful bug report. I'm on OSX 10.6.

> coffee --version             ~/Documents/dev/coffeescript-tests
CoffeeScript version 1.3.1
> coffee random.coffee             ~/Documents/dev/coffeescript-tests
TypeError: In random.coffee, Object 
Splat
  Value "state" has no method 'isArray'
    at Param.names (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:1907:24)
    at Code.paramNames (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:1819:39)
    at Code.compileNode (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:1708:20)
    at Code.compile (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:48:21)
    at Assign.compileNode (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:1519:24)
    at Assign.compile (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:48:21)
    at Block.compileNode (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:311:23)
    at Block.compileWithDeclarations (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:395:19)
    at Code.compileNode (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:1800:35)
    at Code.compile (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/nodes.js:48:21)

@choice([1]) takes > 1 minute while @choice([1,2]) takes less than a second

I'm using Random.choice() to select from a list of one or more items generated at random. Quite frequently the lists were of length 1, but the script would stall on the choice. I can work around it by adding a length check, and taking the [0] of the list if it is length 1 and otherwise using choice, but it seems like an issue the library might face a lot.

I think any function that depends upon @_randbelow will suffer from this problem too.

I've already forked and started writing a fix. Is there a test script?

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.