Java Math.random to get a random double digit between two number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java Math.random to get a random double digit between two number

If i want a random digit between 5.0 and 10.0 using Java Math.random, am i suppose to use Math.random()*5 +5? Note that i want to include 10.0 as well

17th Feb 2022, 3:53 PM
Jacky
2 Answers
+ 1
Math.random() *(10.0-5.0+1 ) + 5.0 ; ( this generates greater than 5.0 and less than 11.0) , if you don't want above 10.0 then don't add +1. it's Same farmulae as the previous one: (int) (Math.random() *(Max-Min+1) +Min) If you want integer type (without fraction part) cast to int.
17th Feb 2022, 5:58 PM
Jayakrishna 🇮🇳
17th Feb 2022, 5:56 PM
Avinesh
Avinesh - avatar