3

Math.floor() rounds the decimal to "A number representing the largest integer less than or equal to the specified number." So Math.floor(45.03) will be 45 and Math.floor(-34.23) will be 35.

And Math.random generates a (decimal, or whole) number between 0, inclusive and 1 exclusive.

I just learned this:

Math.floor(Math.random()*(max-min+1)+min); 

This will generate a random whole number between max and min inclusive. I can figure out mathematically why it works. Just wondering.

Lets call Math.random(), R; max M;min m. If you just look at the inside:

 R*(M-m+1)+m //or RM-Rm+R+m,

It's obvious the quantity is at least as big as m. But why is it no bigger than M? I assume this works with negative M and m, as well.

theoretisch
  • 1,718
  • 5
  • 24
  • 34
  • Possible duplicate of [How does Math.random() work in javascript?](http://stackoverflow.com/questions/20109006/how-does-math-random-work-in-javascript) – Antony Jan 24 '17 at 15:55
  • @Antony No, not that. – deceze Jan 24 '17 at 15:56
  • So can you figure out mathematically why it works? Then what's the problem? Or you CAN'T figure out it? – edc65 Jan 24 '17 at 15:57
  • This is really less of a programming problem and more something for one of the math sites: http://stackexchange.com/sites#science – deceze Jan 24 '17 at 15:58
  • There is a math tag so... – most venerable sir Jan 24 '17 at 16:03
  • As that tags says: *Mathematics is behind all programming at some level, but math questions here should be specifically related to a programmed implementation.* Your question is purely about the mathematics, independent of any specific implementation. – deceze Jan 24 '17 at 16:05
  • it's the same as the standard function for interpolating between two values - `interp(v0, v1, x) = v0 + (v1 - v0) * x`, given x in range [0..1]. When x = 0, interp = v0, and when x = 1, interp = v0 + v1 - v0 = v1. The only minor difference is that Math.random() is exclusive on the upper bound. – Alnitak Jan 24 '17 at 16:08

2 Answers2

2

Lets assume r is Math.random() which is a number between 0, inclusive and 1 exclusive; which we show it as:

// Let r is shortcut for Math.random()
r => [0 to 1}  // lets [] symbols for inclusive and {} symbols for exclusive

to get a number in a larger scale, we can multiply it with N:

//scale by N:
r * N => [0 to N}

to include N in our range, we can use one number greater than N and round the result:

r * (N+1)        => [ 0 to (N+1) }
floor(r * (N+1)) => [ 0 to N ]     // decimals after N will be removed

So up to now, we reach a good formula: to have a random number between 0 and N (both inclusive), we should use: floor(r * (N+1))

if we shift the equation to start from a min value:

//add `min` to equation:
floor(r * (N+1)) + min => [ min to N+min ]

it is almost finished: consider N+min as max, we have:

N+min = max  =>  N = max-min

replace it in our equation:

floor(r * (max-min+1)) + min => [ min to max ]

Note: It is obvious that we can move the min inside the floor function as it is an integer value and does not have any decimal digits. so we could write it also as:

floor( r * (max-min+1) + min )
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
1

It's pretty easy, but you need to write it down a little wordy:

var min = 10;
var max = 20;
var difference = max - min; // 10
var random = Math.random() // (0...1)
var randomDifference = random * difference; // (0...difference)
var withMinOffset = randomDifference + min; // (10...20)

The above withMinOffset is the random value between min and max. The reason this works is because you know the number cannot be lower than min, so we will always have to add the min to the randomised number. Then we know we want a range, so we can use max - min to get the maximum amount of randomness we can get.

The number will never be bigger than M simply because difference + min === max, which is the upper limit. Multiplying say, 10, by a random number between 0 and 1 will always generate a number between 0 and 10. Adding the min to it will always have a number between min and min + difference.

somethinghere
  • 16,311
  • 2
  • 28
  • 42