+ 2

How can I use random function in java ?

Need a simple java code to print random char or numbers (just for understanding).

17th Sep 2017, 1:15 PM
Hemant Jaiswal
Hemant Jaiswal - avatar
10 Réponses
+ 15
use Math.random() function
17th Sep 2017, 1:21 PM
Geeta P Jerkal
Geeta P Jerkal - avatar
+ 16
u HV to use Math.random() method knw
17th Sep 2017, 2:22 PM
Geeta P Jerkal
Geeta P Jerkal - avatar
+ 2
can you explain using a sample code?
17th Sep 2017, 1:41 PM
Hemant Jaiswal
Hemant Jaiswal - avatar
+ 2
Okay @ Full gamer, I will try it
17th Sep 2017, 1:49 PM
Hemant Jaiswal
Hemant Jaiswal - avatar
+ 2
import java.util.*; public class Random { public static void main(String[] args) { Random dice = new Random(); int num = dice.nextInt(6);//line no.7 System.out.println("Random number generated:- " + num); } } // that code shows me an error on line no.7
17th Sep 2017, 2:15 PM
Hemant Jaiswal
Hemant Jaiswal - avatar
+ 2
oh I have just noticed that I posted same question 2 times lol
17th Sep 2017, 3:37 PM
Hemant Jaiswal
Hemant Jaiswal - avatar
+ 2
thanks guys
17th Sep 2017, 3:54 PM
Hemant Jaiswal
Hemant Jaiswal - avatar
+ 2
@Hemant Jaiswal, you should probably not name your class "Random", as that class already exists and when your try to create an object of the "Random" class, the program doesn't know which "Random" class you're referring to (the one in java.util. or the one you just created). Try to fix it.
18th Sep 2017, 6:45 AM
Danilo
Danilo - avatar
+ 2
ya @ danilo
18th Sep 2017, 3:04 PM
Hemant Jaiswal
Hemant Jaiswal - avatar
0
Hi.....
17th Sep 2017, 6:40 PM
Atul
Atul - avatar