make-random

Make a random value, powered by Crypto

Usage no npm install needed!

<script type="module">
  import makeRandom from 'https://cdn.skypack.dev/make-random';
</script>

README

Make Random

License: MIT Build Status github: version github: last-commit npm: version npm: downloads

About Make Random

Make Random is a lightweight Node.js Cryptographically Secure Pseudo-Random Number Generator (CSPRNG) module to help with generating random numbers and other values using the power of Crypto. Trust your results to be both secure and random!

Getting started

npm install make-random

Examples

Jump-to:

All ranges are inclusive of the passed value(s)

random()

The random() method accepts any integer or integer-like string as an optional single argument. The method returns a true integer between 0 and the argument value (inclusive). This holds true for both positive and negative arguments. If no argument is given, the method defaults to randomly returning 0 or 1.

const { random } = require('make-random')

// Return a random integer between 0 and 200
random(200)
.then(resp => console.log(resp))

// Accept integer or integer-like string
random("200")
.then(resp => console.log(resp))

// Works with negative values
random(-10)
.then(resp => console.log(resp))

// Including negative strings
random("-12")
.then(resp => console.log(resp))

// Defaults to returning 0 or 1
random()
.then(resp => console.log(resp))

coinFlip()

The coinFlip() method accepts no arguments. The method returns a random boolean value.

const { coinFlip } = require('make-random')

// Return a random boolean value
coinFlip()
.then(resp => console.log(resp))

randomInRange()

The randomInRange() method accepts any integers or integer-like strings as optional single or pair arguments. The method returns a true integer between the 2 arguments given. If only 1 argument is given, the random number will be between the argument value and 100. If no arguments are given, the method defaults to returning a random integer between -100 and 100. If the arguements are the same value (-10, "-10"), the method will return an integer between 0 and the passed arguments as if random() was used.

Additionally, the randomInRange() method will accept paired arguments in any order, i.e. both randomInRange(-10, 10) and randomInRange(10, -10) is valid and will return the same results.

const { randomInRange } = require('make-random')

// Return random integer between -42 and 42
randomInRange(-42,42)
.then(resp => console.log(resp))

// Accept integer or integer-like strings
randomInRange(-42,"42")
.then(resp => console.log(resp))

// String position doesn't matter
randomInRange("-42",42)
.then(resp => console.log(resp))

// Order of values does not matter
randomInRange("100", "-5")
.then(resp => console.log(resp))

// Return a random integer between -5 and 100
randomInRange(-5)
.then(resp => console.log(resp))

// Return a random integer between 25 and 100
randomInRange(25)
.then(resp => console.log(resp))

// Return a random integer between 100 and 250
randomInRange(250)
.then(resp => console.log(resp))

// Return random interger between -100 and 100
randomInRange()
.then(resp => console.log(resp))

randomAZString()

The randomAZString() method accepts an integer or integer-like argument, and a boolean to determine upper or lowercase as optional single or pair arguments. It returns an A-Z string of the specified length. If no arguments are given, method defaults to a 10 character uppercase string.

const { randomAZString } = require('make-random')

// Return uppercase 10 character random string
randomAZString()
.then(resp => console.log(resp))

// Return uppercase 15 character random string
randomAZString(15)
.then(resp => console.log(resp))

// Return lowercase 15 character random string
randomAZString(15, false)
.then(resp => console.log(resp))

// Accept integer or integer-like string
randomAZString('20')
.then(resp => console.log(resp))

randomString()

The randomString() method accepts an optional integer or integer-like argument. It returns an alpha-numeric string of the specified length. If no argument is given, method defaults to a 10 character string.

const { randomString } = require('make-random')

// Return 10 character random string
randomString()
.then(resp => console.log(resp))

// Return 15 character random string
randomString(15)
.then(resp => console.log(resp))

// Accept integer or integer-like string
randomString('20')
.then(resp => console.log(resp))

randomCaseString()

The randomCaseString() method accepts an optional integer or integer-like argument. It returns an alpha-numeric randomly mixed-case string of the specified length. If no argument is given, method defaults to a 10 character string.

const { randomCaseString } = require('make-random')

// Return 10 character mixed-case random string
randomCaseString()
.then(resp => console.log(resp))

// Return 15 character mixed-case random string
randomCaseString(15)
.then(resp => console.log(resp))

// Accept integer or integer-like string
randomCaseString('20')
.then(resp => console.log(resp))

randomLatin()

The randomLatin() method accepts an optional integer or integer-like argument. It returns a string with the specified number of random latin words. If no argument is given, method defaults to 5 words. All words start with E or R.

const { randomLatin } = require('make-random')

// Return string with 5 random words
randomLatin()
.then(resp => console.log(resp))

// Return string with 12 random words
randomLatin(12)
.then(resp => console.log(resp))

// Accept integer or integer-like string
randomLatin('20')
.then(resp => console.log(resp))

randomEnglish()

The randomEnglish() method accepts an optional integer or integer-like argument. It returns a string with the specified number of random latin words. If no argument is given, method defaults to 5 words. All words start with E or R.

const { randomEnglish } = require('make-random')

// Return string with 5 random words
randomEnglish()
.then(resp => console.log(resp))

// Return string with 12 random words
randomEnglish(12)
.then(resp => console.log(resp))

// Accept integer or integer-like string
randomEnglish('20')
.then(resp => console.log(resp))

randomUUID()

The randomUUID() method generates v4-compliant CSPRNG UUID

const { randomUUID } = require('make-random')

// Return UUID
// example: DC3A411B-6C86-48D7-B0B3-369EF3CB73EF
randomUUID()
.then(resp => console.log(resp))

License

The MIT License (MIT)

Copyright (c) 2016 John Foderaro

Copyright (c) 2019-2022 Erin Rivas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.