Open AurelioDeRosa opened 8 years ago
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 0
as the default for min
, but I wouldn't know a good max
. Number.MAX_SAFE_INTEGER
seems a bit too much.
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?
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: 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.
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: