-2

I tried to create a function in Javascript that will give a random integer between two integers (including both). But, unfortunately this is not including the first integer. The code is as follows:

const randomEjector = (num1,num2) => {return Math.ceil(Math.random() * (num2 - num1)) + num1}

I am considering the analogy of inequality.
++ Let's consider, (Math.random() will output a no. between 0(including) and 1(excluding)).
Let's say x = Math.random(), then x∈[0,1)
So, 0 ≤ x < 1
0 * (num2-num1) ≤ x * (num2-num1) < 1 * (num2-num1)
Again, x*(num2-num1)∈[0,1*(num2-num1))
Now opening Math.ceil() part:
But, Math.ceil() is like greatest integer function:
So, for (0,1] it will return 1.
for 0 => 0
and for interval ((num2-num1)-1,(num2-num1)),it will return (num2-num1).
So, After Math.ceil() is executed, we should be left with an interval [0,(num2-num1)].
Now, x*num2-num1[0,(num2-num1)]
Finally, let y = x*(num2-num1)
So,0 ≤ y ≤ (num2-num1)
So,0 + num1 ≤ y + num1 ≤ (num2-num1) + num1
Which will give num1 ≤ y + num1 ≤ num2
Let, z = y + num1
So, z ∈ [num1,num2]
Then, why my function is excluding 1st term and making it like (num1,num2]?
Where's the problem?
For instance for console.log(randomEjector(3,4)), it will always give 4 and never 3.

Xeroid
  • 11
  • 2
  • [What are the chances of Math.random returning 0?](https://stackoverflow.com/questions/52089553/what-are-the-chances-of-math-random-returning-0) – Ivar Nov 17 '21 at 08:28

2 Answers2

4

It's unspeakably unlikely that Math.random() will ever return exactly 0, so Math.ceil(Math.random()) will unspeakably likely always return 1.

In other words, randomEjector(3,4) will, once in a very, very blue moon, give you 3.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Ok, that means Math.random() can never generate 0. Thank you. – Xeroid Nov 17 '21 at 08:50
  • @Xeroid No, it _can_ generate 0. The odds are just ridiculously slim. ([Around one in 2^1075](https://stackoverflow.com/a/52095861/479156).) – Ivar Nov 17 '21 at 08:54
  • I got another query too. `let myNumbers = [100,998,997];` `const nestedArray = [];` `const newNumber = myNumbers.map((value)=>{` `if(value>400){` `nestedArray.push(value);` `}` `return nestedArray;` `})` `console.log(newNumber);` I am expecting the output to be **[[ 998, 997 ]]** but getting **[[ 998, 997 ],[ 998, 997 ],[ 998, 997 ]]** – Xeroid Nov 17 '21 at 08:56
  • @Xeroid New questions go [here](https://stackoverflow.com/questions/ask). (That is, after you have done enough research yourself first to find the answer.) – Ivar Nov 17 '21 at 09:00
  • So, if i will run my code for 2^1075 times making my browser lag and then, I will get 3. Of all, it might take more than a year too. – Xeroid Nov 17 '21 at 09:05
  • @Xeroid I'm no mathematician, but I think you'll need more time than the universe exists. And even then, it is random. So there is a decent chance you don't even guess it after that time. – Ivar Nov 17 '21 at 09:13
-1

It returns 4 because you are always ceiling the result, if you want it to return 4 then you need to floor the result.

  • But, Math.ceil() is like greatest integer function. When, 3 is integer, Math.ceil(3) will return 3.Ok,,since, Math.random() will never return 0 and hence, my output will never be 3 and always be 4. I got it now. Thank You. – Xeroid Nov 17 '21 at 08:53