Installation
npm install hasard
Description
Random variables and random nested objects manipulation in javascript
Inspired by :
Features:
- Generate basic types randomly (string, number, boolean, integer)
- Nested random object (array, object, matrix)
- Use distribution to generate numbers (normal, uniform, truncated-normal) and integers (poisson, uniform)
- Add reference + context to fix a random variable value in a local context
- Easy-to-use common operators on random variable (add, substract, divide, multiply, round, ceil, floor)
- Create custom operators
- Change the Pseudorandom number generator
- Stream API
Simple Usage
const h = ; const v = hobject color: hvalue'white' 'yellow' // randomly choose between 2 values size: h // randomly choose an integer between 10 and 20; const values = v;console;// [{color: 'white', size: 12}, {color: 'yellow', size: 18}, {color: 'yellow', size: 17}] const value = v;console;// {color: 'white', size: 13}
Prng
You can customize the Pseudorandom number generator which is Math.random
by default.
const n = hvaluechoices: 'white' 'yellow' prng: <custom prng>
Basic types
h.value
h.value(Array.<Hasard> | Hasard.<Array>)
const v = hvalue'white' 'yellow';
h.value({choices: Array.<Hasard> | Hasard.<Array>, weights}) -> Hasard
const v = hvalue choices: 'white' 'yellow' weights: 075 025;
h.boolean
h.boolean({Hasard.<Number>} probability) -> Hasard.<Boolean>
const v = h; // will be true 20% of the time
h.boolean({prob: Hasard.<Number>}) -> Hasard.<Boolean>
const v = h; // will be true 30% of the time
h.number
h.number({Hasard.<Number>} start, {Hasard.<Number>} end) -> Hasard.<Number>
const v = h;
h.number([{Hasard.<Number>} start, {Hasard.<Number>} end]) -> Hasard.<Number>
const v = h;
h.number({type: String, ...}) -> Hasard.<Number>
Available distribution for numbers are
- normal
- uniform
- truncated-normal
Please Open an issue if you need another distribution
const v = h;
const v = h;
h.integer
h.integer({Hasard.<Integer>} start,{Hasard.<Integer>} end) -> Hasard.<Integer>
const v = h;
h.integer([{Hasard.<Integer>} start,{Hasard.<Integer>} end]) -> Hasard.<Integer>
const v = h;
h.integer({type: String, ...}) -> Hasard.<Integer>
For now, the only available distribution for integer is poisson
, please Open an issue
const v = h;
h.string
h.string({value: Hasard, size: Hasard.<integer>}) -> Hasard.<String>
const v = h;
h.array
h.array({value: Hasard, size: Hasard.<Integer>}) -> Hasard.<Array>
const v = h;
h.array(Array.<Hasard>) -> Hasard.<Array>
const v = h;
h.array({values: Array.<Hasard>, size: Hasard.<Integer>, randomOrder: Hasard.<Boolean>}) -> Hasard.<Array>
// pick 5 digits in a randomOrderconst v = h;
h.object
h.object(Object.<String, Hasard>) -> Hasard.<Object>
const obj = hobject color1 : hvalue'white' 'yellow' color2 : hvalue'black' 'grey';
h.object(Hasard.<Array.>, Hasard) -> Hasard.<Object>
const phoneNumber = hasard; const fullName = hasard; const randomPhoneDirectory = hobjectphoneNumber fullName; randomPhoneDirectory // run 2 times//// { // '+33236972292': 'M Julien FOURNIER',// '+1833509762': 'Mr Quentin DURAND',// '+33210149263': 'Mr Maxime MOREAU',// '+1807872258': 'Mr Julien DURAND',// '+32215961607': 'M Julien DUBOIS'// }// // { // '+32067043361': 'Mr Nicolas MOREL',// '+33898861064': 'Mr Thomas DURAND',// '+33730685919': 'Mr Nicolas MOREL',// '+33723780566': 'M Nicolas FOURNIER',// '+33515400984': 'Mr Quentin DUBOIS'// }
h.matrix
h.matrix({value: Hasard, shape: Hasard.<Array.<Integer>>}) -> Hasard.<Matrix>
create matrix with a specific shape
const v = h;
create random matrix with random values and random size
const v = h;
h.reference
h.reference(Hasard) -> Hasard
A reference is generated only once per objet per run.
Let's take an example of how it can be used
const value = h;const v = h; v;// all values are randomized independently// [[22, 128, 54], [250, 134, 12]] const ref = h;const v = h; v;// reference is reused inside the same run// [[72, 72, 72], [114, 114, 114]]
h.reference({source: Hasard, context: Hasard.<String>}) -> Hasard
When defined with a context, the reference is related to a context.
You can define a context with any Hasard tool, by using {contextName: <name of the context>}
// we will create a grey image in RGB so R = G = Bconst ref = h; // Here we need to use the form h.array({values, contextName})const pixel = h; const img = h v;// reference is reused inside the same pixel// [// [[[12, 12, 12],[145, 145, 145]],[[251, 251, 251],[88, 88, 88]]], // first run,// [[[212, 212, 212],[2, 2, 2]],[[78, 78, 78],[130, 130, 130]]] // second run,//]
Helpers
h.isHasard(Any) -> Boolean
Check if the object is an instance of the hasard library
const value = h; h; // trueh; // false
h.fn(Function(Any, ...)) -> Function(Hasard.<Any>, ...)
Example of use
const refA = h;const refB = h; const addHasard = h; const obj = hobject a: refA b: refB sum: ;
Shortcuts
Hasard provides shortcuts for most common operations
h.add(Hasard.<Number>, Hasard.<Number>, ...) -> Hasard.<Number>
const refA = h;const refB = h; const obj = hobject a: refA b: refB sum: h;
h.add(Hasard.<String>, Hasard.<String>, ...) -> Hasard.<String>
const a = hvalue'M. ' 'Mme ';const b = hvalue'DUPONT' 'DURANT'; h// ['M. DUPONT', 'M. DURANT']
h.substract(Hasard.<Number>, Hasard.<Number>) -> Hasard.<Number>
const refA = h;const refB = h; const obj = hobject a: refA b: refB diff: h;
h.multiply(Hasard.<Number>, Hasard.<Number>) -> Hasard.<Number>
const refA = h;const refB = h; const obj = hobject a: refA b: refB mul: h;
h.divide(Hasard.<Number>, Hasard.<Number>) -> Hasard.<Number>
const refA = h;const refB = h; const obj = hobject a: refA b: refB ratio: h;
h.if(Hasard.<Boolean>, Hasard, Hasard) -> Hasard
const test = h;const poisson = h; const signedPoisson = h
h.round(Hasard.<Number>) -> Hasard.<Integer>
const int = h;
h.floor(Hasard.<Number>) -> Hasard.<Integer>
const int = h;
h.ceil(Hasard.<Number>) -> Hasard.<Integer>
const int = h;
h.concat(Hasard.<Array>, Hasard.<Array>) -> Hasard.<Array>
const int = h;
h.getProperty(Hasard.<Number>, Hasard.<Object> | Hasard.<Array>) -> Hasard
const int = h;
Nested randomness
Using set
method, object properties can be set afterward
Generate a random nested Object
const randomValue = hvalue; const randomInteger = h; const randomString = h; const randomNumber = h; const randomKeys = h; const randomObject = hobject randomKeys randomValue; randomValue; console;
Stream API
const h = ; const v = hobject color: hvalue'white' 'yellow' // randomly choose between 2 values size: h // randomly choose an integer between 10 and 20; const streamValue = v; streamValue