Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Possible (minor) enhancement for Math.random()

#335

LOW-PRIORITY:

Support for String values could be achieved by converting min to a number with parseInt() or the ~~ double-bitwise operator.

(No conversion necessary for max since it is achieved with the subtraction operator.)

This may not be an intended behavior for the method, but I thought I'd offer it as a possibility as it would simplify code where (for example) the arguments passed are being taken from <input> value, or some other source offering a string.

Math.random = function(i_min, i_max) {
    // Convert min to number ---------vv
  var rand = Math_old_random(), min = ~~i_min, max = i_max;
  if (arguments.length == 0)
    return rand;
  
  if (arguments.length == 1)
    var max = min, min = 0;

  return Math.floor(rand * (max-min+1)+min);
};

Using ~~, non-convertible values return a result of 0 instead of NaN. If NaN is preferred, then parseInt() instead.

Possible alternate version:

Math.random = function(min, max) {
    
  if (arguments.length == 0)
    return Math_old_random();
    
  if (arguments.length == 1)
    max = min, min = 0;
    
  return ~~(Math_old_random() * (max-min+1) + ~~min);
};

Or to support more String values:

Math.random = function(min, max) {

  if (arguments.length == 0)
    return Math_old_random();

  if (arguments.length == 1)
    max = min, min = 0;
  else
    min = parseInt(min);

  return ~~(Math_old_random() * (parseInt(max)-min+1)+min);
};

Reported by algorithmic_imperative · August 31st, 2010 @ 12:03 AM

State: resolved
Milestone: 2.0.0 Release
Assigned to: nobody

Activity

  1. MadRabbit
    MadRabbit
    • Milestone set to 2.0.0 Release
    • Tag changed from arguments, math.random, string to math.random, string
    • Milestone order changed from 196625 to 0

    Hi Imperative, a nice trick you've gout in here! Never herd about it before, thanks for that. Will check it out and then include into RightJS 2.0

    September 1st, 2010 @ 10:11 AM

  2. MadRabbit
    MadRabbit
    • State changed from new to resolved

    Seems like working just fine.

    September 1st, 2010 @ 10:50 AM

  3. algorithmic_imperative
    algorithmic_imperative

    MadRabbit - Of the three versions I posted, I guess I'd lean toward the last one. It will offer the most possibilities for string values.

    For example, it could accept values line "50px", which may be useful for passing the value of style properties.

       // Set height to random value between the current height and 250
    element.style.height = Math.random(element.style.height, 250) + "px";
    

    September 1st, 2010 @ 05:31 PM

  4. MadRabbit
    MadRabbit

    hmm... sounds a bit naughty to me, besides you always can call it like that

    element.setHeight(Math.random(element.size().y, 250));
    

    which is more explicitly saying what's going on

    September 1st, 2010 @ 05:43 PM

  5. algorithmic_imperative
    algorithmic_imperative

    MadRabbit - Very true, and perhaps a little naughty. :o) The main point was that using parseInt() would allow for additional valid arguments. Not a big deal, though.

    One question, if you don't mind me going a little off topic here. I noticed in some of your Number methods, you add 0 to this.

    For example:

      min: function(value) {
        return this < value ? value : this + 0;  // 0 is added when returning the Number
      },
    

    Just curious about it.

    September 1st, 2010 @ 08:30 PM

  6. MadRabbit
    MadRabbit

    that's simple type-conversion

    typeof(new Number(8));     // -> 'object'
    typeof(new Number(8) + 0); // -> 'number'
    

    If you simply return this it will carry all those additional methods like a number which sometimes causes problems. So I simply convert it back to a number.

    September 1st, 2010 @ 08:37 PM

  7. algorithmic_imperative

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.