Skip to content

Cryptographically secure random integer in range #29707

Closed
@olalonde

Description

@olalonde

Is your feature request related to a problem? Please describe.

There is no API to obtain a random integer within a range. It's a common enough problem to deal with (e.g. to implement other algorithms like Fisher–Yates shuffle) and easy to get wrong. Most other runtimes/languages implement this in their standard library.

Describe the solution you'd like

// something like:
const randomInt = require('crypto').randomInt

const min = 0
const max = 10
randomInt(min, max, cb)

Describe alternatives you've considered

I'm not sure about the internal implementation but I gave it a stab here: https://github.com/olalonde/crypto-range. http://www.pcg-random.org/posts/bounded-rands.html has a nice overview of different techniques (my implementation is probably not the most efficient).

Metadata

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions