Generating and picking random values.
- Managing Seed Values
- seed(number) - specifies the seed that all following random calls will use
- Generating Random Numbers
- randomInteger(min, max) - inclusive integer between min and max values
- randomInteger(min, max) - inclusive float between min and max values
- Working with Arrays
- pickRandom(array) - picks a value at random from the list
- randomArray(size, fn) - creates an array of size length, with each value generated from fn
- Simplex Noise
- simplexGenerator(seed) - Number generator between -1 and 1 given an x/y/z coordinate
If leveraging
While generating a simple number between two values is common, it is very important - and useful in generating fake data.
const firstNames = ['jane', 'john', 'paul', 'ringo'];
const lastNames = ['do', 'doe', 'dough', 'doh'];
fakeName = `${utils.random.pickRandom(firstNames)} ${utils.random.pickRandom(lastNames)}`;
// 'john dough'
Additionally, there are so many different ways of generating visualizations based on simplex noise.
From straight (red - negative / green - positive)
To indicators with length, and rotation (negative ccw / positive cw)
The possibilities are endless.
This library is meant to provide simple use cases. Please use Standard libraries
- like d3-random for additional alternatives
see format.mapArrayDomain - as it projects a value from between a range a value, and picks the corresponding value from an array.
For example:
require('esm-hook');
d3 = require('d3');
utils = require('jupyter-ijavascript-utils');
//-- create a number generator using Normal / Gaussian distribution
randomGenerator = d3.randomNormal(
0.5, // mu - or centerline
0.1 // sigma - or spread of values
);
randomValue = randomGenerator();
// randomValue - 0.4
randomDataset = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
numPicks = 3; // increase to a larger number (ex: 1000) to better see distributions
//-- create an array of 3 items, each with the results from randomGenerator
results = utils.array.size(numPicks, () => randomGenerator());
// [ 0.6235937672428706, 0.4991359903898883, 0.4279365561645624 ]
//-- map those values to the randomDataset
resultPicks = results.map(val => ({ pick: utils.format.mapArrayDomain(val, randomDataset) }));
// [ { pick: 'g' }, { pick: 'e' }, { pick: 'e' } ]
//-- group them by the pick field
//-- then add a new property called count - using the # of records with the same value
groupedResults = utils.group.by(resultPicks, 'pick')
.reduce((list) => ({ count: list.length }));
// [ { pick: 'g', count: 1 }, { pick: 'e', count: 2 } ]
//-- make a bar chart (only with 10k results)
utils.vega.embed((vl) => {
return vl
.markBar()
.title('Distribution')
.data(groupedResults)
.encode(
vl.x().fieldN('pick'),
vl.y().fieldQ('count').scale({type: 'log'})
);
});
Methods
(static) pickRandom(targetArray) → {any}
- See:
-
d3-random for additional alternatives
Picks a random value from an array of values (with uniform distribution)
Example
utils.random.pickRandom(['apple', 'orange', 'pear']); // 'pear'
Parameters:
Name | Type | Description |
---|---|---|
targetArray |
Array | Array of values to pick from |
Returns:
- one of the values picked at random from the target array
- Type
- any
(static) random(minopt, maxopt) → {Number}
- See:
-
d3-random for additional alternatives
Generates a random floating point number (with uniform distribution)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
min |
Number |
<optional> |
0
|
Minimum float (inclusive) that could be generated |
max |
Number |
<optional> |
100
|
Maximum float (inclusive) number that the value will be less than |
Returns:
- number between (and including) min and max utils.random.randomInteger(0, 1) // 0.224223
- Type
- Number
(static) randomArray(arraySize, generatingFunctionopt) → {Array}
Generates an array of random values
Example
utils.random.randomArray(4); // [0.23, 0.56, 0.87, 0.77];
utils.random.randomArray(4, () => utils.random.randomInteger(1, 100)); // [22, 11, 99, 32]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arraySize |
Number | length of the array to return |
||
generatingFunction |
function |
<optional> |
null
|
function to use to generate or number 0-1 |
Returns:
- array of size arraysize
- Type
- Array
(static) randomInteger(minopt, maxopt) → {Number}
- See:
-
d3-random for additional alternatives
Generate a random integer (with uniform distribution)
Example
utils.random.randomInteger(0, 100) // 40
utils.random.randomInteger(0, 100) // 96
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
min |
Number |
<optional> |
0
|
Minimum integer (inclusive) that could be generated |
max |
Number |
<optional> |
100
|
Maximum integer (inclusive) number that could be generated |
Returns:
- number between (and including) min and max
- Type
- Number
(static) seed(newSeedValue)
Specifies a new seed to be used in upcoming random calls.
Example
utils.random.seed(12345);
utils.random.randomInteger(); // 55
Parameters:
Name | Type | Description |
---|---|---|
newSeedValue |
Number | new seed to use in following random calls |
(static) simplexGenerator(seedopt) → {SimplexGenerator}
Returns a new Simplex Generator
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
seed |
Number |
<optional> |
seed to use for the generator (or null for random) |
Returns:
- Simplex Generator that can generate in multiple dimensions
- Type
- SimplexGenerator