+ 2
How can I use random function in java ?
Need a simple java code to print random char or numbers (just for understanding).
10 Answers
+ 15
use Math.random() function
+ 16
u HV to use Math.random() method knw
+ 2
can you explain using a sample code?
+ 2
Okay @ Full gamer, I will try it
+ 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
+ 2
oh I have just noticed that I posted same question 2 times lol
+ 2
thanks guys
+ 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.
+ 2
ya @ danilo
0
Hi.....