Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: ff297f5aedde
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 920a6c2b144c
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Aug 11, 2015

  1. Optimize rand with a range

    Creating an array out of the range and then choosing a random element is
    slick, but it performs pretty badly for wide ranges. :-)
    jgaskins committed Aug 11, 2015
    Copy the full SHA
    e27551d View commit details
  2. Merge pull request #1052 from jgaskins/optimize-rand-range

    Optimize rand with a range
    elia committed Aug 11, 2015
    Copy the full SHA
    920a6c2 View commit details
Showing with 3 additions and 2 deletions.
  1. +3 −2 opal/corelib/kernel.rb
5 changes: 3 additions & 2 deletions opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -1003,9 +1003,10 @@ def rand(max = undefined)
return Math.random();
}
else if (max.$$is_range) {
var arr = #{max.to_a};
var min = max.begin, range = max.end - min;
if(!max.exclude) range++;
return arr[#{rand(`arr.length`)}];
return self.$rand(range) + min;
}
else {
return Math.floor(Math.random() *