Skip to content

[Meta] Math.randomInt(min, max) #40

Open
@AurelioDeRosa

Description

@AurelioDeRosa

Generating random numbers is a very common task in any language. By looking at discussions on StackOverflow such as [1] and [2] and the relevant MDN page, it's clear that many developers would benefit from such method. Not that it's really hard to implement using Math.random() but other utility methods have been added to JavaScript to facilitate developers' life.

My preference would go to the version that includes both the boundaries. So, the implementation should be something like:

Math.randomInt = function(min, max) {
   return Math.floor(Math.random() * (max - min + 1)) + min;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions