java random number
import java.util.Random;
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;
java random number
import java.util.Random;
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;
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);
Random number generator in java
// java.util.random example
import java.util.Random;
public class JavaRandomClass
{
public static void main(String[] args)
{
Random random = new Random();
// random integers in range 0 to 999
int randInt1 = random.nextInt(1000);
int randInt2 = random.nextInt(1000);
// printing random integers
System.out.println("Random Integers: " + randInt1);
System.out.println("Random Integers: " + randInt2);
// here we are generating Random doubles
double randDou1 = random.nextDouble();
double randDou2 = random.nextDouble();
// printing random doubles
System.out.println("Random Doubles: " + randDou1);
System.out.println("Random Doubles: " + randDou2);
}
}
java random usage
import java.util.Random;
Random rndm = new Random();
int mnmbr = 10;
int rndNumber = rndm.nextInt(mnumber) + 1;
System.out.println(rndNumber);
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us