Code Monkey home page Code Monkey logo

jquery-prng's Introduction

jquery-prng

jquery mersenne-twister pseudorandom number generator pluggin

demo: https://angeal185.github.io/jquery-prng

Installation

npm

$ npm install jquery-prng --save

bower

$ bower install jquery-prng

browser

<script src="./path-to/jquery.min.js"></script>
<script src="./dist/jq-prng.min.js"></script>

info

the seed is 128bit, generated from an array of random integers and then put through a fisher yates shuffle.

// accepted methods
[
  'abs','acos','acosh','asin','asinh','atan',
  'atanh','cbrt','ceil','clz32','cos','cosh',
  'exp','expm1','floor','fround','log','log1p',
  'log10','log2','round','sign','sin','sinh',
  'sqrt','tan','tanh','trunc'
]

// accepted operators
[
  '+','-','/','*'
]
/**
* $(ele).prng(['round', '*', 100])
* @param {ele} string ~ jquery valid element selector
* @param {array} config ~ optional | config options
* [string, string, number] ||
* [string, string, number, string, number]
*/

/* element */
// add prng to element | .val(input/textarea) | .text(!input/!textarea)
// return $(element).val(Math.round(prng))
$('input').prng()

// add prng between 1-100 to element | .val(input/textarea) | .text(!input/!textarea)
// return $(element).text(Math.round(prng) * 100)
$('body').prng(['round', '*', 100])

// add prng between 1-50 devided by 2 to element | .val(input/textarea) | .text(!input/!textarea)
// return $(element).val(Math.round(prng) * 50 / 2)
$('textarea').prng(['round', '*', 50, '/', 2])


/* sync */
/**
* $.prngSync(config)
* @param {array} config ~ optional | config options
* [string, string, number] ||
* [string, string, number, string, number]
*/

// return random base prng
let sync = $.prngSync();
console.log(sync);

// return Math.round(prng) * 10;
sync = $.prngSync(['round', '*', 10])
console.log(sync);

// return Math.round(prng) * 100 - 2;
sync = $.prngSync(['round', '*', 100, '-', 2])
console.log(sync);


/* callback */
/**
* $.prng(config,cb)
* @param {array} config ~ optional | config options
* @param {function} cb ~ callback function(err,res)
* [string, string, number] ||
* [string, string, number, string, number]
*/

// return random base prng
$.prng(function(err, res){
  if(err){return console.log(err)}
  console.log(res)
});

// return Math.floor(prng) * 100;
$.prng(['floor', '*', 100], function(err, res){
  if(err){return console.log(err)}
  console.log(res)
});

// return Math.floor(prng) * 10 / 2;
$.prng(['floor', '*', 10, '/', 2], function(err, res){
  if(err){return console.log(err)}
  console.log(res)
});

/* promise */
/* sync */
/**
* $.prngP(config)
* @param {array} config ~ optional | config options
* [string, string, number] ||
* [string, string, number, string, number]
*/

// return random base prng
$.prngP().then(function(res){
  console.log(res)
}).catch(function(err){
  console.log(err)
})

// return Math.fround(prng) - 100;
$.prngP(['fround', '-', 100]).then(function(res){
  console.log(res)
}).catch(function(err){
  console.log(err)
})

// return Math.fround(prng) * 1000 + 200;
$.prngP(['fround', '*', 1000, '+', 200]).then(function(res){
  console.log(res)
}).catch(function(err){
  console.log(err)
})


/* numbered string */

/* sync */
/**
* $.prngStrSync(num)
* @param {number} num ~ number string length;
*/

// return random string of 3 numbers;
sync = $.prngStrSync(3)
console.log(sync)


/* callback */
/**
* $.prngStr(num, cb)
* @param {number} num ~ number string length;
* @param {function} cb ~ callback function(err,res)
*/

// return random string of 10 numbers;
$.prngStr(10,function(err,res){
  if(err){return console.log(err)}
  console.log(res)
})


/* promise */
/**
* $.prngStrP(num)
* @param {number} num ~ number string length;
*/

// return random string of 5 numbers;
$.prngStrP(5).then(function(res){
  console.log(res)
}).catch(function(err){
  console.log(err)
})


/* numbered array */

/* sync */
/**
* $.prngStrSync(al,nl)
* @param {number} al ~ array length;
* @param {number} nl ~ number length;
*/

// return random numbered array;
sync = $.prngArrSync(3,4)
console.log(sync)


/* callback */
/**
* $.prngStr(al, nl, cb)
* @param {number} al ~ array length;
* @param {number} nl ~ number length;
* @param {function} cb ~ callback function(err,res)
*/

// return random numbered array;
$.prngArr(10,12,function(err,res){
  if(err){return console.log(err)}
  console.log(res)
})


/* promise */
/**
* $.prngStrP(al,nl)
* @param {number} al ~ array length;
* @param {number} nl ~ number length;
*/

// return random numbered array;
$.prngArrP(5,5).then(function(res){
  console.log(res)
}).catch(function(err){
  console.log(err)
})

jquery-prng's People

Contributors

angeal185 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.