• Stars
    star
    112
  • Rank 302,555 (Top 7 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate random numbers from different probability distributions.

Prob.js bower npm LICENSE

by Andrew Brampton 2016

Generate random numbers from different probability distributions. Demo.

Use

Bower:

bower install prob.js
<script src="bower_components/random/lib/random.min.js" type="text/javascript" ></script>
<script src="bower_components/prob.js/dist/prob-min.js" type="text/javascript" ></script>

Node.js:

npm install prob.js
var Prob = require('prob.js');

Example:

var r = Prob.normal(0, 1.0); // μ = 0, σ = 1.0 
r(); // Returns a random number from this distribution
r(); // Returns another random number
r(); // and again

API

The following distribution are available:

Prob.uniform(min, max) // Uniform distribution in range [min, max).
Prob.normal(μ, σ)      // Normal distribution with mean and standard deviation.
Prob.exponential(λ)    // Exponential distribution with lambda.
Prob.lognormal(μ, σ)   // Log-normal distribution defined as ln(normal(μ, σ)).
Prob.poisson(λ)        // Poisson distribution returning integers >= 0.
Prob.zipf(s, N)        // Zipf's distribution returning integers in range [1, N].

After generating a distribution, the following methods are available:

var r = Prob.exponential(1.0); // Create a distribution.
r()        // Generates a number within the distribution.
r(src)     // Generates a number using a `src` of random numbers. (See note below.)
r.Min      // The min random number which could be returned by `r()` (inclusive).
r.Max      // The max random number which could be returned by `r()` (exclusive).
r.Mean     // The expected mean for this distribution.
r.Variance // The expected variance for this distribution.

Random source

Internally Prob.js uses Mersenne Twister provided by random-js. This can be overridden by providing the src argument when generating a number. src is expected to be a function that when called returns a signed integer uniformly in the range [-2^31,2^31).

For example:

// https://xkcd.com/221/
function xkcd_source() {
	return 4; // chosen by fair dice roll.
	          // guaranteed to be random.
};

var r = Prob.exponential(1.0); // Create a distribution.

// Use the XKCD source
console.log( r(xkcd_source) );

// Or use a better source (supplied by random-js)
console.log( r(Random.engines.browserCrypto) );

// Or just use the default which happens to be Random.engines.mt19937().autoSeed()
console.log( r() );

How to release

make clean && make   # Build and test once
mversion patch       # Bump version number (v1.2.3 | major | minor | patch)
make clean && make   # Be extra sure after the version bump it continues to work

git add -f bower.json package.json dist/{prob-min.js,prob-min.js.map,prob.js}
VERSION=v`mversion | tail -n 1 | cut -d ' ' -f 2`
git commit -m "Releasing version $VERSION"
git tag $VERSION
git push origin
git push origin --tags

npm publish          # Publish to npm (publishing to bower is not needed)

Licence (Apache 2)

This is not an official Google product (experimental or otherwise), it is just code that happens to be owned by Google.

Copyright 2016 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

js-sequence-diagrams

Draws simple SVG sequence diagrams from textual representation of the diagram
JavaScript
7,764
star
2

ffmpeg-cli-wrapper

Java wrapper around the FFmpeg command line tool
Java
1,551
star
3

unsafe

Assorted java classes that make use of sun.misc.Unsafe
Java
97
star
4

antlr4-grammars

Precompiled Go versions of most of the grammars on https://github.com/antlr/grammars-v4.
Go
97
star
5

libcec-daemon

A Linux daemon for connecting libcec to uinput. That is, using your TV to control your PC!
C++
64
star
6

myip

(A better) What's My IP Address web application
Go
54
star
7

archivemount

FUSE filesystem using libarchive (this is out of date copy of:
C
43
star
8

protoc-gen-php

*DEPRECATED* A PHP Protocol Buffer Generator Plugin for protoc - Please use the now official compiler https://github.com/google/protobuf/tree/master/php
C++
42
star
9

Connected-component-labelling

A javascript library for connected-component labelling (aka blob extraction)
JavaScript
21
star
10

rustdns

DNS Client in Rust
Rust
19
star
11

goredirects

A tool to help host go packages with vanity domains on GitHub
Go
17
star
12

goadvent-antlr

Examples from my goadvent blog post
Go
17
star
13

dsector

API for parsing binary files using a predefined grammar
Go
14
star
14

smart-health-card-scanner

Scan Smart Health Cards
JavaScript
14
star
15

dataflow-art

Go
12
star
16

morebeam

Additional functions useful for building Apache Beam pipelines in Go.
Go
11
star
17

node-ioctl

Simple nodejs addon to add ioctl
C++
10
star
18

objectgraph

Simple library to traverse Java object graph
Java
10
star
19

ndsfs

A FUSE application to mount Nintendo DS roms
C
9
star
20

hessian.js

Node.js support for the Hessian binary web service protocol
JavaScript
8
star
21

funcs

Google Cloud Functions
JavaScript
7
star
22

mysqldump2csv

Convert mysqldump files to CSVs
Go
7
star
23

pmtiles-dart

pmtiles decoder for dart
Dart
7
star
24

p2psim

A peer-to-peer DHT simulator
Java
5
star
25

curriculum-vitae

Andrew Brampton's Curriculum Vitae
Makefile
5
star
26

jambox

Tool to interact with the Jawbone Jambox
C++
4
star
27

libarchive

C library and command-line tools for reading and writing tar, cpio, zip, ISO, and other archive formats
C
4
star
28

flvtool-plus-plus

flvtool++
C++
4
star
29

js-scan

Attempt to scan the Internet and determine what libraries everyone is using
JavaScript
3
star
30

libphonenumber-php

PHP Bindings for libphonenumber
C++
3
star
31

snow.js

A simple snow demo (using device orientation)
JavaScript
3
star
32

jackson-datatype-commons-lang3

Simple Jackson library for some datatypes in Apache Commons Lang3
Java
3
star
33

db-patterns

Some simple DB patterns implemented onto of MySQL
Java
3
star
34

dissector

Dissects various file formats
Java
3
star
35

nodewii-talk

A talk I gave on nodewii at NodeDC Nov 2012 and later at NationJS 2012
JavaScript
2
star
36

ssltester

Tests how well a SSL service is set up, and alerts if it's below required threshold
Go
2
star
37

php5-spotify

This is a simple PHP extension that wraps some of the functionality of libspotify.
C
2
star
38

ByteTorrent

Some old code I wrote to speak BitTorrent
C++
2
star
39

us-census-tools

A collection of scripts I've written to handle census data.
PHP
2
star
40

jPsyScript

PsyScript (written in Java)
Java
2
star
41

threadnetperf

Threadnetperf is a highly customisable high performance multi-threaded network benchmarking tool
C
2
star
42

sms-rpc

Simple Android app to explose a RESTful API to control your device, such as for sending SMS.
Java
2
star
43

libmintchip

Unofficial C library for interfacing with MintChips - Unfinished, and mostly an exercise in reverse engineering a protocol
C
2
star
44

bithacks

A collection of bit twiddling hacks in dart
Dart
2
star
45

php-sdk

PHP SDK for the Facebook API
PHP
1
star
46

blog

My blog (work in progress)
HTML
1
star
47

khan-index

Index of Khan Academy
Go
1
star
48

ultimate-tic-tac-toe

Rust
1
star
49

bounce

Bouncing Ball Game (unfinished)
1
star
50

aoc

Advent Of Code
Rust
1
star
51

godot-test

Test Repo for Godot
HTML
1
star
52

nine_patch

Dart
1
star
53

solvers

Solvers for sudoku and more
Go
1
star
54

par-rs

Rust PAR File implementation
Rust
1
star
55

is_x86_feature_detected

Rust
1
star
56

lusernet

LUSerNet - a peer-to-peer network that runs over a LAN.
Visual Basic
1
star
57

handy-tools

A collection of helpful cli tools written in python
Python
1
star
58

publication

A selection of my publications (with source tex/resources/etc)
TeX
1
star
59

yii-bootstrap

A clone of the mercurial yii-bootstrap - https://bitbucket.org/Crisu83/yii-bootstrap
PHP
1
star
60

js-sudoku

Sudoku solver written in javascript which uses computer vision to extract the sudoku from a image
CSS
1
star
61

phd-thesis

Andrew Brampton's PhD Thesis
TeX
1
star
62

categories

Randomly generates categories
JavaScript
1
star
63

sms-count

Simple app to count the length of a SMS message using different encodings
CSS
1
star