-
Notifications
You must be signed in to change notification settings - Fork 26
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
Comments
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. |
I think the most common use cases suggest |
I like this idea but i actually think |
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. |
@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. |
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: 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. |
Thank you for considering this proposal @leobalter. I'll look forward for some feedback. |
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. |
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. |
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:
The text was updated successfully, but these errors were encountered: