# randomstring

> A module for generating random strings

Latest version **1.3.1** (published 2025-01-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install randomstring
pnpm add randomstring
yarn add randomstring
bun add randomstring
```

Provides the command `randomstring`.

## Health

**Score 33/100 (F)** — status: maintenance-mode.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2025-01-10 |
| First published | 2012-01-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/randomstring) |
| Module format | CommonJS |
| Node | * |
| Dependencies | 1 |
| Unpacked size | 16.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 521 |
| Author | Elias Klughammer |
| Maintainers | eliaskg |

## Links

- npm: https://www.npmjs.com/package/randomstring
- Repository: https://github.com/klughammer/node-randomstring
- Issues: https://github.com/klughammer/node-randomstring/issues
- npm.io page: https://npm.io/package/randomstring

## Dependencies (1)

- [randombytes](https://npm.io/package/randombytes.md) 2.1.0

## Recent versions

- 1.3.1 (latest) — 2025-01-10
- 1.3.0 — 2023-06-02
- 1.2.3 — 2022-10-20
- 1.2.2 — 2022-01-22
- 1.2.1 — 2021-05-10
- 1.2.0 — 2021-05-10
- 1.1.5 — 2016-05-18
- 1.1.4 — 2016-02-10
- 1.1.3 — 2015-11-03
- 1.1.2 — 2015-11-03
- 1.1.0 — 2015-09-06
- 1.0.8 — 2015-09-01
- 1.0.7 — 2015-07-03
- 1.0.6 — 2015-06-01
- 1.0.5 — 2015-04-03
- … 5 more at https://npm.io/package/randomstring/versions

## README

# node-randomstring

[![Build Status](https://travis-ci.org/klughammer/node-randomstring.svg?branch=master)](https://travis-ci.org/klughammer/node-randomstring) [![Download Stats](https://img.shields.io/npm/dm/randomstring.svg)](https://github.com/klughammer/node-randomstring)

Library to help you create random strings.

## Installation

To install randomstring, use [npm](http://github.com/npm/npm):

```
npm install randomstring
```

## Usage

```javascript
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.

---
_Source: https://npm.io/package/randomstring · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
