A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Math.random Update


Does it hurt for this to be possible:

Math.random(1, 5); // returns a float 1-5 inclusive

Perhaps a third Boolean argument for whether it returns integer eliminating the use of Math.floor();

As of right now Math.random() called with arguments ignores the arguments and does nothing.

We are trying to improve javascript, this should be an easy fix right? It’s just a function so?


This is not a good idea, for a general and more specific reason:

  1. General for all programming languages: http://ariya.ofilabs.com/2011/08/hall-of-api-shame-boolean-trap.html.

  2. JS-specific: JS does not enforce arity so it’s possible (you’d be surprised what happens that is allowed – almost everything) that code on the Web already calls Math.random with extra actual arguments (perhaps via .apply or .call). In general ECMA-262 reserves extra parameters to future specs, but in reality it’s very hard to extend extant methods this way.



I’ve remember learning about why Boolean parameters are bad, and thanks for the article. The only question I have about that is:

Take Element.cloneNode() without passing the Boolean argument is that one a bad design?

I’m trying to understand why Element.cloneNode(false) would mean not to clone it? What else would it mean, what’s the point of calling it? Or is this a good example of using a Boolean for a parameter.

So should Element.cloneNode(true) be changed to Element.cloneNode({deep: true}). or something similar.

Ok now on Math.random() really?!?! it’s called with arguments, yet the arguments are ignored?

You said with call or apply so:

Math.random.call(Math, 1, 2, 'whateverBecauseThisIsIgnored');
Math.random.apply(Math, [1, 2, 'whateverBecauseThisIsIgnored');

Or how else would it be?

So what if it were like this: Math.random({min: 1, max: 2, intergersOnly: true});

I apologize if I sound angry Brendan, I’m not, I’m just trying to understand more just in case I encounter something else like this?


It would likely be safer to add properties to the method itself:

  Math.random.bounded(1, 5, otherThing)

Or follow the other native paradigm of making a constructor:

  var randomInstance = new Math.Random();
  randomInstance.bounded(1, 2, etc);

Various libraries use call and apply internally for reasons such as passing arguments through, it can be the case that these mistakenly pass through the arity wrong mistakenly to functions that don’t need it as part of a factory method etc.

However Chrome seem to be pushing ‘strong mode’ to solve arity abuse in the long term which is a nice benefit.


Is there a function that already does that, I can’t think of anything that is a native function which has a method on it. I’m talking about:

Math.random.bounded(1, 5, otherThing)

Math.random is a native function. Is there native method that has a method on it?


Most natives objects for a start:

new Array()
new Date()

I feel there are lots of older ones that do the same (I wasn’t advocating it really, merely suggesting a similar API out of scope would be better than changing arity)


Ok that are technically not constructors because those are technically constructors.

And by the way:

var randomInstance = Math.Random();
randomInstance.bounded(1, 2, etc);

Wouldn’t work because Math.random() returns an immutable number.


They can behave slightly differently is my point, that and JavaScript doesn’t really care much:

new Boolean(0)
//Boolean {[[PrimitiveValue]]: false}

Note the case on Random.


Also I meant to put new Math.Random() edited. :sleepy:


Do you really think the committee would let that happen? because I don’t see any pattern in JS like that as of right now.


No probably not as it has been a use case for so long, mostly the answer has been… deal with it.

history, History, and location, Location are both examples of property access and their classes.

Intl and Intl.Collator is a better example though.