2

I wrote a function:

function rand(minDit, maxDit){
  return Math.floor(Math.random() * (maxDit - minDit + 1)) + minDit; 
}

I use it to generate a random integer from minDit to maxDit, I want to know if there is a better solution ?

Matt
  • 22,721
  • 17
  • 71
  • 112
Jimco
  • 21
  • 2
  • 3
    @Jimco: anything wrong with your code? Does it solve the issue it should solve? Any **real technical reason** to search for "the better" solution? – zerkms Sep 21 '12 at 05:14
  • 1
    @venkateshwar: `0` is a valid value in this case. – zerkms Sep 21 '12 at 05:15
  • @venkateshwar: what "same" problem? ;-) I don't see any problems explanation in the question. – zerkms Sep 21 '12 at 05:19
  • 1
    zerkms— the +1 is required if the range includes the maximum value and `Math.floor` is being used. Using round will include the minimum and maximum values but at a lower probability than all other numbers in the range (roughly half since they will only occur for round down or round up respectively, whereas others will occur for rounding up and down). – RobG Sep 21 '12 at 05:22
  • @RobG: yep, now I see the difference, thanks – zerkms Sep 21 '12 at 05:26
  • @zerkms—the initial number is (roughly) evenly distributed in the range but round affects that. e.g. you'll get zero for values from 0 to 0.49999999, but you'll get 1 for values from 0.5 to 1.49999999, which is twice the range. Same for the max value. – RobG Sep 21 '12 at 05:28
  • @RobG: yep, I got it right after my initial comment revision. That's why I edited it after ;-) – zerkms Sep 21 '12 at 05:28
  • `i=new Date%100` instead of `i=0|Math.random()*100` is NOT a good idea in case someone is considering that possibilit[y](https://github.com/jed/140bytes/wiki/Byte-saving-techniques) – ajax333221 Sep 21 '12 at 05:32
  • I was playing with [this code](http://jsbin.com/edajal/5/edit) in a [previous question](http://stackoverflow.com/questions/12523180/a-random-number-between-1-and-4-thats-not-another-random-number/12523496#12523496). It may be what you need. – elclanrs Sep 21 '12 at 05:36
  • @elcanrs The code you reference is not guarenteed to complete it is just highly likely to. It also is extremely inefficient for narrow ranges of relatively high numbers, say random numbers from 2^50 - 2^51. – chuckj Sep 21 '12 at 05:40
  • 3
    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random#Example.3A_Using_Math.random – Felix Kling Sep 21 '12 at 05:42
  • @FelixKling You should make that the answer (after putting words around it so SO accepts it). – chuckj Sep 21 '12 at 05:45
  • @chuckj: Yeah I know won't be good for huge numbers, it's not meant for that... but how would you make it work with large numbers faster? – elclanrs Sep 21 '12 at 05:46
  • @elclanrs Use the rand() presented in this (edited) question. However, the use of indexOf() means ranges that are the equal or nearly equal to the range size will take a very long time. Your code only performs well on small ranges of random numbers where there array produced is significanly smaller than the range (or trivially short). – chuckj Sep 21 '12 at 05:53
  • @chuck Would it be a good idea to then recursively call the function in chunks of let's say 10000 and then join the results? It wouldn't be completely random for ranges of 10000 or more tho... – elclanrs Sep 21 '12 at 06:06
  • @elclanrs I feel we have strayed too far from the original question. Feel free to e-mail me directly if you want to continue discussing this. – chuckj Sep 21 '12 at 06:11
  • Don't worry about it I'm just playing with this code. I might hit you up if I find a solution. xD – elclanrs Sep 21 '12 at 06:17

0 Answers0