how to random genrate nummbers 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: how to create a random number in java
import java.util.Random;
class scratch{
public static void main(String[] args) {
Random rand = new Random();
System.out.println( rand.nextInt(5) );
}
}
Example 3: generate random number in java
import java.lang.Math;
int randomNumber;
int minimum = 1;
int maximum = 10 - minimum;
randomNumber = (int)((Math.random()*maximum) + minimum);
System.out.println("Random Number (Between 1-10): " + randomNumber);
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);
}
}