What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

How to Generate Random Numbers in Java

Sophia

New member
Joined
Aug 27, 2022
Messages
18
Reaction score
0
HTML Coins
0

Random Number Generation with Java​

Random number can be generated using two ways. java.util.Random class is used to generate random numbers of different data types such as boolean, int, long, float, and double. An object of Random class is initialized and the method nextInt(), nextDouble() or nextLong() is used to generate random number. You can also use Math.Random to generate random value between 0.0 and 1.0.
Let’s look at how to generate 10 random numbers in Java –

Example: Using Java Random Class​

First, we will see the implementation using java.util.Random – Assume we need to generate 10 digit random number in Java between 0 to 100.

import java.util.Random;
public class RandomNumbers{
public static void main(String[] args) {
Random objGenerator = new Random();
for (int iCount = 0; iCount< 10; iCount++){
int randomNumber = objGenerator.nextInt(100);
System.out.println("Random No : " + randomNumber);
}
}
}
Output:

Random No : 17
Random No : 57
Random No : 73
Random No : 48
Random No : 68
Random No : 86
Random No : 34
Random No : 97
Random No : 73
Random No : 18

An object of Random class is initialized as objGenerator. The random number generator in Java class has a method as nextInt. This will provide a random number based on the argument specified as the upper limit, whereas it takes lower limit is 0.Thus, we get 10 random numbers displayed.

Example: Using Java Math.Random​

Now, if we want 10 random numbers generated java but in the range of 0.0 to 1.0, then we should make use of random number generator Java class called math.random() .


You can use the following loop to generate them-

public class DemoRandom{
public static void main(String[] args) {
for(int xCount = 0; xCount< 10; xCount++){
System.out.println(Math.random());
}
}
}
Output:

0.46518450373334297
0.14859851177803485
0.5628391820492477
0.6323378498048606
0.1740198445692248
0.9140544122258946
0.9167350036262347
0.49251219841030147
0.7426056725722353

Now, you know how those strange numbers are generated!!!

Summary:​

Random number can be generated using two ways. You can use Random Java class (in package java.util) or Using Math.random Java class (however this will generate double in the range of 0.0 to 1.0 and not integers).
 
Extend form Using the java.lang.Math class:

Code:
public class RandomNumberGenerator {
    public static void main(String[] args) {
        // Generate a random double between 0.0 (inclusive) and 1.0 (exclusive)
        double randomDouble = Math.random();


        // Generate a random integer between 0 and 99
        int randomIntInRange = (int) (Math.random() * 100);


        System.out.println("Random Double: " + randomDouble);
        System.out.println("Random Integer in Range: " + randomIntInRange);
    }
}

Thanks
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back