To use the random function, we need to import
import java.util.Random ;
or
import java.util.* ;
and then we need to create the constructor as follows:
Random r = new Random() ;
and then we can use the datatype as follows:
int i = r.nextInt(int n) between 0 to n
int i = r.nextInt() all integers
long l = r.nextLong() all long
float f = r.nextFloat() between 0.0 and 1.0
double d = r.nextDouble() between 0.0 and 1.0
boolean b = r.nextBoolean() either true or false
No comments:
Post a Comment