Possible (minor) enhancement for Math.random()
Reported by algorithmic_imperative | August 31st, 2010 @ 12:03 AM | in 2.0.0 Release
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);
};
Comments and changes to this ticket
-
MadRabbit September 1st, 2010 @ 10:11 AM
- 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
-
MadRabbit September 1st, 2010 @ 10:50 AM
- State changed from new to resolved
Seems like working just fine.
-
algorithmic_imperative September 1st, 2010 @ 05:31 PM
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";
-
MadRabbit September 1st, 2010 @ 05:43 PM
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
-
algorithmic_imperative September 1st, 2010 @ 08:30 PM
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 add0
tothis
.For example:
min: function(value) { return this < value ? value : this + 0; // 0 is added when returning the Number },
Just curious about it.
-
MadRabbit September 1st, 2010 @ 08:37 PM
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.
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
RightJS Core Tickets