Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AurelioDeRosa opened this issue Jun 7, 2016 · 10 comments
Open

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

AurelioDeRosa opened this issue Jun 7, 2016 · 10 comments
Labels

Comments

@AurelioDeRosa
Copy link
Collaborator

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;
};
@jzaefferer
Copy link
Collaborator

I like the idea, since its easy enough to mess up the conversion from float to int.

I think you'd have to define what the method does with just one or zero arguments. If there are no defaults, throwing an error should be fine as well.

@AurelioDeRosa
Copy link
Collaborator Author

I think the most common use cases suggest 0 as the default for min, but I wouldn't know a good max. Number.MAX_SAFE_INTEGER seems a bit too much.

@arschmitz
Copy link
Collaborator

I like this idea but i actually think Number.MAX_SAFE_INTEGER seems correct for max if there is no max supplied. Why limit it?

@leobalter
Copy link
Collaborator

This is interesting, but I believe if I search on esdiscuss, I can find some previous convo on this to understand why it didn't advance.

If we can't find any information, the best way is to talk to TC39 and see if it would have a chance. Hopefully yes. With that, we can start drafting the proposal docs including spec parts for it.

@AurelioDeRosa
Copy link
Collaborator Author

@leobalter: I took a look at esdiscuss and I wasn't able to find a related discussion

@arschmitz: No real reason, I guess I just needed someone to validate my initial thought.

@bkardell
Copy link
Collaborator

bkardell commented Jun 8, 2016

@leobalter
Copy link
Collaborator

One thing I would change is the params order:

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

One interesting use for this method is allowing an easy learning curve using the language, and setting only the max value makes it more interesting, like: Math.randomInt(10).

As I mentioned in the other issue, I'll bring this to a informal talk with other TC39 representatives. If everything goes ok we transform this in a formal proposal.

@AurelioDeRosa
Copy link
Collaborator Author

Thank you for considering this proposal @leobalter. I'll look forward for some feedback.

@leobalter
Copy link
Collaborator

I didn't find much love for this in the last meeting. I still want to see if this could be possible, but I don't want to present it as a new proposal as the chance to get it rejected is still high and that could block it from new attempts in a short time.

@AurelioDeRosa
Copy link
Collaborator Author

Thank you for the update Leo. It's such a shame considering how much this function is used and how many discussions you can find on the web.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants