• Stars
    star
    165
  • Rank 228,254 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Probability.js makes it easy to call JavaScript functions by probability in Node.js and the browser.

Probability.js Logo

What is it?

Probability.js makes it easy to call JavaScript functions by probability in Node.js and the browser.

Why?

Circumstances are rare that you need to call functions by a certain probability in your daily work. But sometimes, especially in game development and in statistical applications, it's very handy to have an easy way of doing so. This library is inspired by this question on stackoverflow.com.

Overview

Probabilitilized functions

A probabilitilized function are several functions combined to one function by probability objects. It is created by calling the constructor Probability() with probability objects as arguments:

var probabilitilized = new Probability(probabilityObject, probabilityObject /* , ... */);

or with an array of probability objects:

var probabilitilized = new Probability([probabilityObject, probabilityObject /* , ... */]);

Probability objects

A probability object consists of an object with the properties p and f. p is the probability by that the function f is called when the probabilitilized function is invoked.

The value of the probability p have to be a string with an integer value suffixed with % between 0 and 100 or preferred a float lesser than or equal to 1.0. f is an ordinary JavaScript function:

var probabilityObject = {
    p: '50%',
    f: function () {}
};

/* or */

var probabilityObject = {
    p: 0.5,
    f: function () {}
};

The sum of all probabilities p must be lesser or equal to 100% or, respectively, 1.0. Otherwise a TypeError is thrown. That's also the case for malformed probabilityObjects.

Usage example

/* a counter to show the number of function calls */
var counter = {
    0: 0,
    1: 0,
    2: 0
};

/* create a "probabilitilized function" by invoking Probability() with 3 "probability objects" */
var probabilitilized = new Probability({
    p: '30%',                                         /* the probability by that ... */
    f: function () {                                  /* ... this function is called */
        counter[0]++;
    }
}, {
    p: '10%',
    f: function () {
        counter[1]++;
    }
}, {
    p: '60%',
    f: function () {
        counter[2]++;
    }
});

/* call the probabilitilized functions 100 times */
for (var i = 0; i < 100; i++) {
    probabilitilized();
}

/* show that every function is called by its probability */
console.log(counter); // => {"0":27,"1":11,"2":62}

More Repositories

1

Stately.js

Stately.js is a JavaScript based finite-state machine (FSM) engine for Node.js and the browser.
JavaScript
814
star
2

jquery-behavior

Unobstrusive scripting simply and easily. Declarative events with Metabehaviors based on Live Query.
JavaScript
15
star
3

pushover.sh

Shell script to push messages to Android and iOS devices via Pushover (https://pushover.net/)
Shell
12
star
4

event-stream-proxy

A open reverse proxy allowing to poll JSON REST APIs and push updates to clients by utilizing Server-Sent Events (SSE)
JavaScript
7
star
5

tado-server-proto

Protocol for JSON-based communication via HTTP GET between Android App and tado server
7
star
6

Prescaler

Arduino Prescaler Library
4
star
7

bas-for-little-wire

Bas is an interpreter for the classic dialect of the programming language BASIC extended for use with the "Little-Wire - minimal AVR programmer and more"
C
4
star
8

spdns-client.sh

Shell script to update DNS records on http://spdns.de/
Shell
3
star
9

TypeSafe.js

A library for creating typed functions in JavaScript.
JavaScript
2
star
10

thinlet

Fork of Thinlet AWT GUI framework
Java
2
star
11

libarduino

C library for using standard GNU tools with Arduino hardware.
C
2
star
12

rtl_sdr

A private rtl_sdr clone with synchronous single threaded rtl_fm
C
2
star
13

gtalkbot

a simple xmpp bot with plugin support
C
1
star
14

PICAXE_SerialLCD

PICAXE example for the Seeed Technology UART based LCD1602 display (Hitachi HD44780 or a compatible LCD).
Visual Basic
1
star
15

ComunioPlusPlusDE

Extending the comunio.de basic player account. ;)
JavaScript
1
star
16

usbfunkd

A RESTful webserver that let you control 433MHz wireless remote sockets via a webinterface. See https://github.com/MalteP/usbfunk
C
1
star
17

PICAXE_Makefile

Makefile for using the PICAXE command line console compilers
1
star
18

SerialLCD

Seeedstudio SerialLCD library for Arduino 1.0
Java
1
star
19

snippets.js

Various JavaScript snippets.
JavaScript
1
star
20

node-red-contrib-authed-websocket

A websocket input and output node that rejects connections based on an external authentication flow.
TypeScript
1
star
21

infoBox

A jQuery infoBox plugin
JavaScript
1
star
22

android-samsung-3.0-jb

C
1
star
23

EZLib

EZLib is a C utility library, which provides some useful data types, string utilities and memory handling macros. It also provides macros for using object oriented programming in ISO C and a simple implementation of structured exception handling with try/catch blocks.
Shell
1
star