UNPKG

ngraph.random

Version:

Operation with random numbers for ngraph.*

1 lines 2.75 kB
(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.ngraphRandom=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){module.exports=random;module.exports.random=random,module.exports.randomIterator=randomIterator;function random(inputSeed){var seed=typeof inputSeed==="number"?inputSeed:+new Date;return new Generator(seed)}function Generator(seed){this.seed=seed}Generator.prototype.next=next;Generator.prototype.nextDouble=nextDouble;Generator.prototype.uniform=nextDouble;Generator.prototype.gaussian=gaussian;function gaussian(){var r,x,y;do{x=this.nextDouble()*2-1;y=this.nextDouble()*2-1;r=x*x+y*y}while(r>=1||r===0);return x*Math.sqrt(-2*Math.log(r)/r)}Generator.prototype.levy=levy;function levy(){var beta=3/2;var sigma=Math.pow(gamma(1+beta)*Math.sin(Math.PI*beta/2)/(gamma((1+beta)/2)*beta*Math.pow(2,(beta-1)/2)),1/beta);return this.gaussian()*sigma/Math.pow(Math.abs(this.gaussian()),1/beta)}function gamma(z){return Math.sqrt(2*Math.PI/z)*Math.pow(1/Math.E*(z+1/(12*z-1/(10*z))),z)}function nextDouble(){var seed=this.seed;seed=seed+2127912214+(seed<<12)&4294967295;seed=(seed^3345072700^seed>>>19)&4294967295;seed=seed+374761393+(seed<<5)&4294967295;seed=(seed+3550635116^seed<<9)&4294967295;seed=seed+4251993797+(seed<<3)&4294967295;seed=(seed^3042594569^seed>>>16)&4294967295;this.seed=seed;return(seed&268435455)/268435456}function next(maxValue){return Math.floor(this.nextDouble()*maxValue)}function randomIterator(array,customRandom){var localRandom=customRandom||random();if(typeof localRandom.next!=="function"){throw new Error("customRandom does not match expected API: next() function is missing")}return{forEach:forEach,shuffle:shuffle};function shuffle(){var i,j,t;for(i=array.length-1;i>0;--i){j=localRandom.next(i+1);t=array[j];array[j]=array[i];array[i]=t}return array}function forEach(callback){var i,j,t;for(i=array.length-1;i>0;--i){j=localRandom.next(i+1);t=array[j];array[j]=array[i];array[i]=t;callback(t)}if(array.length){callback(array[0])}}}},{}]},{},[1])(1)});