Created
December 15, 2018 01:32
-
-
Save shawnsi/c7922cc386917fdc98466aa782a20f37 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.poisson = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | |
var Process = require('./lib/Process'); | |
// Raw sampling function | |
exports.sample = require('./lib/sample'); | |
// Semantic version, useful for inspection when version is not known. | |
exports.version = require('./lib/version'); | |
// .create style contstructor | |
exports.create = function (avgIntervalMs, triggedFn) { | |
return new Process(avgIntervalMs, triggedFn); | |
}; | |
},{"./lib/Process":2,"./lib/sample":3,"./lib/version":4}],2:[function(require,module,exports){ | |
var sample = require('./sample'); | |
var Process = function (interval, fn) { | |
// Parameters: | |
// interval | |
// number of milliseconds | |
if (typeof interval !== 'number') { | |
throw new Error(interval + ' should be a number.'); | |
} | |
if (typeof fn !== 'function') { | |
throw new Error('Callee ' + fn + ' should be a function.'); | |
} | |
if (interval < 0) { | |
throw new Error(interval + ' should be a non-negative number.'); | |
} | |
this.interval = interval; | |
this.fn = fn; | |
this.timeout = null; | |
}; | |
Process.prototype.start = function () { | |
var dt = sample(this.interval); | |
var self = this; | |
this.timeout = setTimeout(function () { | |
self.start(); | |
self.fn(); | |
}, dt); | |
}; | |
Process.prototype.stop = function () { | |
clearTimeout(this.timeout); | |
}; | |
module.exports = Process; | |
},{"./sample":3}],3:[function(require,module,exports){ | |
module.exports = function (mean) { | |
// Generate exponentially distributed variate. | |
// | |
// Inter-arrival times of events in Poisson process | |
// are exponentially distributed. | |
// mean = 1 / rate | |
// Math.log(x) = natural logarithm of x | |
return -Math.log(Math.random()) * mean; | |
}; | |
},{}],4:[function(require,module,exports){ | |
// generated by genversion | |
module.exports = '1.0.0'; | |
},{}]},{},[1])(1) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment