Search

How to Generate Random Number in Java || Java Programs

In real world projects, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many applications use the OTP. The best example of random numbers is dice. Because when we throw it, we get a random number between 1 to 6. So, in this blog post we will understand how to generate random number program in java by various methods.

Random Number

Random numbers are the numbers that use a large set of numbers and selects a number using the mathematical algorithm. It satisfies the following two conditions:

• The generated values uniformly distributed over a definite interval.

• It is impossible to guess the future value based on current and past values.

Generating Random Number in Java

In Java, there is four ways to generate random numbers using the method and classes.

• Using the random() Method

• Using the Random Class

• Using the ints() Method (in Java 8)

RandomNumberExample1.java

```import java.lang.Math;
public class RandomNumberExample1
{
public static void main(String args[])
{
// Generating random numbers
System.out.println("1st Random Number: " + Math.random());
System.out.println("2nd Random Number: " + Math.random());
System.out.println("3rd Random Number: " + Math.random());
System.out.println("4th Random Number: " + Math.random());
}
}  ```

Output -

```1st Random Number: 0.17477777777
2nd Random Number: 0.42988888888
3rd Random Number: 0.48999999999
4th Random Number: 0.13267912333```

RandomNumberExample2.java

```public class RandomNumberExample2
{
public static void main( String args[] )
{
int min = 200;
int max = 400;
//Generate random double value from 200 to 400
System.out.println("Random value of type double between "+min+" to "+max+ ":");
double a = Math.random()*(max-min+1)+min;
System.out.println(a);
//Generate random int value from 200 to 400
System.out.println("Random value of type int between "+min+" to "+max+ ":");
int b = (int)(Math.random()*(max-min+1)+min);
System.out.println(b);
}
}  ```

Output -

```Random value of type double between 200 to 400:
233.8832980277777
Random value of type int between 200 to 400:
32900```

RandomNumberExample3.java

```import java.util.Random;
public class RandomNumberExample3
{
public static void main(String args[])
{
// creating an object of Random class
Random random = new Random();
// Generates random integers 0 to 49
int x = random.nextInt(50);
// Generates random integers 0 to 999
int y = random.nextInt(1000);
// Prints random integer values
System.out.println("Randomly Generated Integers Values");
System.out.println(x);
System.out.println(y);
// Generates Random doubles values
double a = random.nextDouble();
double b = random.nextDouble();
// Prints random double values
System.out.println("Randomly Generated Double Values");
System.out.println(a);
System.out.println(b);
// Generates Random float values
float f=random.nextFloat();
float i=random.nextFloat();
// Prints random float values
System.out.println("Randomly Generated Float Values");
System.out.println(f);
System.out.println(i);
// Generates Random Long values
long p = random.nextLong();
long q = random.nextLong();
// Prints random long values
System.out.println("Randomly Generated Long Values");
System.out.println(p);
System.out.println(q);
// Generates Random boolean values
boolean m=random.nextBoolean();
boolean n=random.nextBoolean();
// Prints random boolean values
System.out.println("Randomly Generated Boolean Values");
System.out.println(m);
System.out.println(n);
}
}

```

Output -

```Randomly Generated Integers Values
46103
8885
Randomly Generated Double Values
0.37823000000000000
0.9980581729999
Randomly Generated Float Values
0.878046103
0.931031010
Randomly Generated Long Values
-497482355555555559
3650240138444444666
Randomly Generated Boolean Values
false
true```

RandomNumberExample4.java

```import java.util.concurrent.ThreadLocalRandom;
public class RandomNumberExample4
{
public static void main(String args[])
{
// Generate random integers between 0 to 999
// Print random integer values
System.out.println("Randomly Generated Integer Values:");
System.out.println(a);
System.out.println(b);
// Generate Random double values
// Print random doubles
System.out.println("Randomly Generated Double Values:");
System.out.println(c);
System.out.println(d);
// Generate random boolean values
// Print random Booleans
System.out.println("Randomly Generated Boolean Values:");
System.out.println(e);
System.out.println(f);
}
}  ```

Output -

```Randomly Generated Integer Values:
1234567895
-1887000000
Randomly Generated Double Values:
0.156444456565656
0.524299966355888
Randomly Generated Boolean Values:
false
false```

Tags:

Related Posts

See All

How to Print Pattern in Java || Java Programs

Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program

Armstrong Number in Java || Java Programs

Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc. Let's try to understand why 153 is an

Factorial Program in Java || Java Programs

Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 Factorial Program us