Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

randomstring

klughammer2.8mMIT1.3.1TypeScript support: definitely-typed

A module for generating random strings

readme

node-randomstring

Build Status Download Stats

Library to help you create random strings.

Installation

To install randomstring, use npm:

npm install randomstring

Usage

var randomstring = require("randomstring");

randomstring.generate();
// >> "XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT"

randomstring.generate(7);
// >> "xqm5wXX"

randomstring.generate({
  length: 12,
  charset: 'alphabetic'
});
// >> "AqoTIzKurxJi"

randomstring.generate({
  charset: 'abc'
});
// >> "accbaabbbbcccbccccaacacbbcbbcbbc"

randomstring.generate({
  charset: ['numeric', '!']
});
// >> "145132!87663611567!2486211!07856"

randomstring.generate({
  charset: 'abc'
}, cb);
// >> "cb(generatedString) {}"

API

randomstring.

  • generate(options, cb)
    • options
      • length - the length of the random string. (default: 32) [OPTIONAL]
      • readable - exclude poorly readable chars: 0OIl. (default: false) [OPTIONAL]
      • charset - define the character set for the string. (default: 'alphanumeric') [OPTIONAL]
        • alphanumeric - [0-9 a-z A-Z]
        • alphabetic - [a-z A-Z]
        • numeric - [0-9]
        • hex - [0-9 a-f]
        • binary - [01]
        • octal - [0-7]
        • custom - any given characters
        • [] - An array of any above
      • capitalization - define whether the output should be lowercase / uppercase only. (default: null) [OPTIONAL]
        • lowercase
        • uppercase
    • cb - Optional. If provided uses async version of crypto.randombytes

Command Line Usage

$ npm install -g randomstring

$ randomstring
> sKCx49VgtHZ59bJOTLcU0Gr06ogUnDJi

$ randomstring 7
> CpMg433

$ randomstring length=24 charset=github readable
> hthbtgiguihgbuttuutubugg

Tests

npm install
npm test

LICENSE

node-randomstring is licensed under the MIT license.

changelog

1.3.1 / Jan 10, 2025

  • Updated randombytes

1.3.0 / Jun 02, 2023

  • Added support for multiple character sets
  • Removed dependency on array-uniq

1.2.3 / Oct 20, 2022

  • Fixed React support
  • Fixed unexpected behavior of length option

1.2.2 / Jan 22, 2022

  • Fixed browser support

1.2.1 / May 10, 2021

  • Fixed tests

1.2.0 / May 10, 2021

  • Use randombytes instead of node.crypto to prevent biased output
  • Add support for async generation
  • Support for binary and octal charsets

1.1.5 / May 18, 2016

  • Optimized character generation algorithm

1.1.4 / Feb 10, 2016

  • Added option for capitalization

1.1.3 / Nov 03, 2015

  • Fixed test

1.1.2 / Nov 03, 2015

  • Added command line support
  • Fixed bug causing the "readable" option to fail

1.1.0 / Sep 06, 2015

  • Added support for custom character sets
  • Added option for excluding poorly readable characters

1.0.8 / Sep 01, 2015

  • Avoid problems if crypto.randomBytes throws an exception

1.0.7 / Jul 03, 2015

  • Use node.crypto instead of Math.random as random number generator

1.0.6 / Jun 01, 2015

  • Added licence for npmjs.org
  • Enhanced readme for Github and npm

1.0.5 / Apr 03, 2015

  • Better charset setting → Less error-proneness

1.0.4 / Apr 03, 2015

  • Added tests

1.0.3 / Feb 17, 2014

  • Fixed typo in character set

1.0.0 / Jan 21, 2012

  • Start of the project