Random module for JavaScript
JSrandom.randFloat([from,] to)
from (Number): The lower bound of range, defaut is zero .
to (Number): The upper bound of range.
(Number): Returns a random number between [from,to).
JSrandom . randFloat ( 5 ) ; // <=> JSrandom.randFloat(0,5)
//=>1.4567891701389055
JSrandom . randFloat ( - 2 , 3 ) ;
//=>-0.37792440940529315
JSrandom.randInt([from,] to)
from (Number): The lower bound of range, defaut is zero .
to (Number): The upper bound of range.
(Integer): Returns a random integer between [from,to).
JSrandom . randInt ( 5 ) ; // <=> JSrandom.randInt(0,5)
//=>2
JSrandom . randInt ( - 2 , 3 ) ;
//=>-1
JSrandom.select(array [, generator])
array (ArrayLike): The array to select.
generator (function): A random number generator.defaut is the build-in uniform generator use MT19937
(any): Returns a random element in the array.
JSrandom . select ( "abcdefg" ) ;
//=>"f"
JSrandom . select ( [ 0 , 2 , 3 , 5 , 2 , 9 , 1 ] , Math . random ) ;
//=>5
Equal to JSrandom.select(array), use the built-in random number generator.
JSrandom.sample(array [,number])
array (ArrayLike): The array to select.
number (Number): The element number to pick, defaut value is a random number between [1,array.length) .
(Array): A random subset of the array.
JSrandom . sample ( "abcdefg" ) ;
//=>["b", "e", "c", "a", "d", "f", "g"]
JSrandom . sample ( [ 0 , 2 , 3 , 5 , 2 , 9 , 1 ] , 3 ) ;
//=>[5, 0, 2]
array (ArrayLike): The array to shuffle.
(Array): A shuffled list, do not change the origin array.
JSrandom . shuffle ( "abcdefg" ) ;
//=>"bacdegf"
JSrandom . shuffle ( [ 0 , 2 , 3 , 5 , 2 , 9 , 1 ] ) ;
//=>[0, 3, 2, 9, 5, 2, 1]
JSrandom.Uniform(lower,upper)
Constructor of the random number generator obey uniform distribution.
lower (Number): The lower bound of range.
upper (Number): The upper bound of range.
(function): An instance of Uniform which generate uniform random numbers between lower and upper.
var generator = JSrandom . Uniform ( 0 , 10 ) ;
generator ( ) ;
//=>2.0685795052296387
JSrandom.Gaussian(mu,sigma)
Constructor of the random number generator obey gaussian distribution.
mu (Number): The expectation of gaussian distribution.
sigma (Number): The variance of gaussian distribution.
(function): An instance of Gaussian which generate random number obey normal distribution.
var generator = JSrandom . Gaussian ( 0 , 1 ) ;
generator ( ) ;
//=>0.6532581496839591
Constructor of the random number generator obey bernoulli distribution.
prob (Number): The probability of true.
(function): An instance of Bernoulli which return true or false.
var generator = JSrandom . Bernoulli ( 0.5 ) ;
generator ( ) ;
//=>true
generator ( ) ;
//=>false
JSrandom.Binomial(upper,prob)
Constructor of the random number generator obey binomial distribution.
upper (Integer): The upper bound of range.
prob (Number): The probability of success.
(function): An instance of Binomial which return integer [ 0, upper ] , the probability obey the binomial distribution.
var generator = JSrandom . Binomial ( 10 , 0.5 ) ;
generator ( ) ;
//=>8
generator ( ) ;
//=>3
JSrandom.Cauchy(location,scale)
Constructor of the random number generator obey cauchy distribution.
location (Number): The location of cauchy distribution.
scale (Number): The scale of cauchy distribution.
(function): An instance of Cauchy which return random number obey the cauchy distribution.
var generator = JSrandom . Cauchy ( 3 , 1 ) ;
generator ( ) ;
//=>5.677625315054479
generator ( ) ;
//=>-10.669827067906697
JSrandom.Exponential(lamda)
Constructor of the random number generator obey exponential distribution.
lamda (Number): The index coefficient of Exponential distribution.
(function): An instance of Exponential which return random number obey the exponential distribution.
var generator = JSrandom . Exponential ( 3 ) ;
generator ( ) ;
//=>0.21951388774858185
generator ( ) ;
//=>0.15475914346331018
Other Random Distributions
negativeBinomial(times,prob)
(Number): random number between between [0, 1) and obey uniform distribution.
JSrandom . uniform ( ) ;
//=>0.22049420430347985
JSrandom.gaussian(mu,sigma)
mu (Number): The expectation of gaussian distribution, defaut value is 0 .
sigma (Number): The variance of gaussian distribution, defaut value is 1 .
(Number): random number obey normal distribution.
JSrandom . gaussian ( ) ;
//=>1.7041861226289101
prob (Number): The probability of true, defaut value is 0.5 .
JSrandom . bernoulli ( 0.8 ) ;
//=>true
JSrandom.exponential(lamda)
lamda (Number): The index coefficient of Exponential distribution.
(Number): random number obey the exponential distribution.
JSrandom . exponential ( 3 ) ;
//=>0.29350827394207385
###Other generator