Math.random() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Math.random()

Is the following line creates random number from 0 to 9 int n=(int)(Math.random*10);

2nd Dec 2018, 11:22 PM
mn121
mn121 - avatar
2 Answers
+ 6
Hey, import java.util.*; outside your class and use the following.👍 int r = new Random().nextInt(10); System.out.print(r);
3rd Dec 2018, 12:54 AM
D_Stark
D_Stark - avatar
+ 1
@D_Stark If you know you are using only Random class from util package, it is best practice to import only the Random class. For example: import java.util.Random; To summarize, import only the packages you need.
4th Dec 2018, 12:14 AM
Darshak Mehta
Darshak Mehta - avatar