Randomization utilities in Node or the browser. About as random as you can get natively.
Disclaimer: This lib does not claim to be fully cryptographically secure. Use at your own risk. No guarantee of output uniqueness or contextual suitability is given or implied.
npm install random-entities
import random from 'random-entities'
random.generate()
// => 0.181188155933931
Seven methods are provided which can also be imported stand-alone
generate
char
id
uuid
integer
float
fromArray
Generates a 52-bit mantissa number, similar to Math.random()
, using window.crypto.getRandomNumbers
.
RandomEntities.generate(<void>)
Module import:
random-entities/core-rng
<Number>
between 0 and 1 (exclusively)
RandomEntities.generate()
// => 0.7676222995416488
Generates a string of one or more characters.
RandomEntities.char(length)
Module import:
random-entities/char
-
length <Number>
| optional | default:1
The length of the string to generate.
<String>
A string of random characters.
RandomEntities.char()
// => "z"
RandomEntities.char(12)
// => "bjdzywvmatsb"
Generates a unique alpha-numeric identifier string. The first character is always a letter. Optionally, numeric characters can be omitted.
RandomEntities.id(length, alpha)
Module import:
random-entities/id
-
length <Number>
| optional | default:8
| minimum:4
The length of the string to generate.
-
alpha <Boolean>
| optional | default:false
If
true
, only letters will be returned (equivalent to thechar()
method)
<String>
A string of random characters.
RandomEntities.id()
// => "w29a892b"
RandomEntities.id(3)
// => "v489"
RandomEntities.id(6, true)
// => "ryvadn"
Generates a 128-bit hexadecimal universally unique identifier*
*Although effort has been put into making this as close as possible to a true UUID, no guarantees are made in regards to their true uniqueness. Use at your own risk.
RandomEntities.uuid(<void>)
Module import:
random-entities/uuid
<String>
A 128-bit hexadecimal UUID
RandomEntities.uuid()
// => "ba4eb3e4-9d35-424c-9513-f7cbbc69070b"
Generates a random whole-number integer between the given limits, ceiling optionally inclusive. Limits may be positive or negative numbers, but may not be logically reversed (eg min: 4, max: 2)
RandomEntities.integer(min, max, inclusive)
Module import:
random-entities/integer
-
min <Number>
| requiredThe lowest possible result, inclusively
-
max <Number>
| requiredThe highest possible result, exclusively by default
-
inclusive <Boolean>
| optional | default:false
If
true
, the highest possible number is included in the result pool
<Number>
A random number between the given minimum and maximum, exclusively by default.
RandomEntities.integer(0, 100)
// => 41
RandomEntities.integer(1, 2)
// => always 1 since 2 is exclusive
RandomEntities.integer(15, 20, true)
// => 17 but could return 20
Generates a random floating-point number between the given limits, ceiling optionally inclusive. Limits may be positive or negative numbers, but may not be logically reversed (eg min: 0.4, max: 0.6)
RandomEntities.float(min, max, prevision, inclusive)
Module import:
random-entities/float
-
min <Number>
| requiredThe lowest possible result, inclusively
-
max <Number>
| requiredThe highest possible result, exclusively by default
-
max <Number>
| optional | default:2
The number of decimals to keep.
-
inclusive <Boolean>
| optional | default:false
If
true
, the highest possible number is included in the result pool.
<Number>
A random floating-point number between the given minimum and maximum, exclusively by default.
RandomEntities.float(2, 3)
// => 2.07
RandomEntities.float(200, 300, 6)
// => 268.887126
RandomEntities.float(0, 10, 1, true)
// => 8.5 but could return 10.0
Selects a random entry from an array or string.
RandomEntities.fromArray(list)
Module import:
random-entities/from-array
-
list <Array|String>
| requiredThe array from which to select an item.
<Any>
A random selection from the array.
RandomEntities.fromArray(['one', 'two', 1, 2])
// => "two"
RandomEntities.fromArray([{ prop: 'value' }, [1, 2, 3]])
// => { prop: "value" }
RandomEntities.fromArray('antidisestablishmentarianism')
// => "r"