generating random number in java code example
Example 1: java random number
import java.util.Random;
Random rand = new Random();
int n = rand.nextInt(50);
n += 1;
Example 2: java random usage
import java.util.Random;
Random rndm = new Random();
int mnmbr = 10;
int rndNumber = rndm.nextInt(mnumber) + 1;
System.out.println(rndNumber);
Example 3: generate random number java
(Math.random() * ((max - min) + 1)) + min
Example 4: random number generator java
import java.util.Random;
class GenerateRandom {
public static void main( String args[] ) {
Random rand = new Random();
int upperbound = 25;
int int_random = rand.nextInt(upperbound);
double double_random=rand.nextDouble();
float float_random=rand.nextFloat();
System.out.println("Random integer value from 0 to" + (upperbound-1) + " : "+ int_random);
System.out.println("Random float value between 0.0 and 1.0 : "+float_random);
System.out.println("Random double value between 0.0 and 1.0 : "+double_random);
}
}