Closed pixelzoom closed 8 years ago
Related to https://github.com/phetsims/phet-io/issues/551.
My recommendation would be to rename randomIntegerBetween
to nextBetween
.
There are currently 68 usages of randomIntegerBetween
.
And while we're at it... Maybe we should simplify this by requiring both min
and max
params, and just getting rid of the falsy
param. I don't see a lot of value in having the same function signature as _.random
, and it adds complexity.
Of the 68 usages of randomIntegerBetween
, I see only 3 (all in fraction-matcher) that are omitting the min
arg. And (imo) those should all be using nextInt
instead.
E.g. LevelModel line 111:
var scaleFactor = numericScaleFactors[ phet.joist.random.randomIntegerBetween( numericScaleFactors.length - 1 ) ]; //random scaleFactor
... would be clearer as:
var scaleFactor = numericScaleFactors[ phet.joist.random.nextInt( numericScaleFactors.length ) ]; //random scaleFactor
In the above commits, I:
I see only 3 (all in fraction-matcher) that are omitting the min arg. And (imo) those should all be using nextInt instead.
I haven't done this yet, instead I converted the 1-arg calls to use nextBetween( 0, ...
. After doing this, I see 9 cases that match this pattern, all of which are candidates for nextInt()
.
phet.joist.random.randomIntegerBetween( 1.3, 5.2 ) 3.3
I'm somewhat worried about this because with that method name it seems like it could return any floating point number between 1.3 and 5.2. Whereas with the implementation, it could only return integral steps above the min, which are: 1.3, 2.3, 3.3, 4.3, 5.3. NOTE this last value is out of range. Should we rename the method to indicate it is designed for integers? Should we assert that the values passed in are integers? Should we provide another method designed for floating point? It would be crazy to provide one method with this behavior: nextBetween(0,3) => 0,1,2,3 nextBetween(0.5,0.6) => 0.500000000001, 0.50000000000002, etc.... 0.599999999, 0.6
Right now my only remedy for this situation is the elaboration in the JSDoc which says:
* Randomly select a random value an integral number of steps above min (inclusive).
But I am concerned with a name like nextBetween()
this method will be misinterpreted and used incorrectly for floating points.
It would be crazy to provide one method with this behavior
WOW, this is exactly what lodash does:
This sounds like bugs waiting to happen.
My vote would be to require nextBetween
to have integer args, and rename it nextIntBetween
, for symmetry with nextInt
.
Fixed above, @pixelzoom can you take a peek?
Replaced Number.isInteger with DOT.Util.isInteger. Back to @samreid for review.
@samreid I still see 10 usages of randomIntegerBetween
and 2 sims are failing (BLL, RPAL). Looks like you missed some renaming.
randomIntegerBetween
renamed to nextIntBetween
for BLL and RPAL in above commits.
I ran the load tester after your changes and everything seems like it is working now, and usage of DOT.Util.isInteger looks good. Anything else to do for this issue?
Nothing else to do, closing.
(1) Documentation
Random line 118:
Lots of questions here... Why does this mention underscore? Do you mean lodash? And exactly which function are you matching?
I think what you probably mean is:
(2) Return type and function name
Note that lodash's
_.random
documentation (https://lodash.com/docs#random) says:Whether intentional or not, this seems to be supported in the current implementation. Testing in the console:
So
randomIntegerBetween
is a poor name for this, because it doesn't necessarily return an integer.