0

How to get a random digit between two number ? Using Java Math.random method

For example, if i want to get a random digit between 15 and 50, am i suppose to use Math.random()*35 +15 or Math.random()*36 +15? Like i want to include 50 as well because it is "between"

17th Feb 2022, 3:32 PM
Jacky
4 Respostas
+ 1
In this code can you see how it can be done: https://code.sololearn.com/c2Hf4XKQ0QDJ/?ref=app
17th Feb 2022, 3:45 PM
JaScript
JaScript - avatar
+ 1
I hope this link helps you. https://code.sololearn.com/cz6uAw03zg64 int range = max - min + 1; int rand = (int)(Math.random() * range) + min;
17th Feb 2022, 3:52 PM
SoloProg
SoloProg - avatar
0
(int) (Math.random() *(50-15+1) + 15) edit:Jacky (int) (Math.random() *(Max-Min+1) +Min) If you want double type (fraction values,) don't cast to int.
17th Feb 2022, 3:46 PM
Jayakrishna 🇮🇳
0
What if i want a digit between 2.0 and 10.0? What will be solution using Math.random???
17th Feb 2022, 3:48 PM
Jacky