View Single Post
 12-03-2012, 09:50 PM PM User | #6 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts This way of getting random numbers is *WRONG*: Code: ```var rands1 = Math.ceil((Math.random()*4)); var randn1 = Math.ceil((Math.random()*13));``` In each case, there is a small but FINITE CHANCE that the number you get will be ZERO! That's because there is a small but finite chance that Math.random() can produce a zero value. Multiply zero by anything and then take the Math.ceil() of it and you still have zero. The *CORRECT* formula is Code: ```var rands1 = 1 + Math.floor(Math.random()*4); var randn1 = 1 + Math.floor(Math.random()*13);``` In general the formula is: Code: ```function getRandomInt( low, high ) { return low + Math.floor( Math.random() * ( high - low + 1 ) ); }``` (which code assumes low and high are themselves integers). __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.