Understanding Random Java Class with Examples
Java is one of the richest software development languages in terms of its built-in features. Owing to these rich features, Java is one of those languages that still comprise major portion enterprise level software application. Java comes with a variety of classes that support almost all the basic application development features. String classes provide capability for manipulating strings without having to write string manipulation functions in bits and pieces. Similarly, the Math class in Java provides support for performing mathematical functions. Another extremely important and interesting class that is used to generate random pseudo-numbers is the Java Random class. This article briefly describes the structure and functionality performed by the Java Random class, with the help of examples.
Interested in learning more about Java? Look at a Udemy.com course.
Why Random Number Generation is important?
A software application often needs to perform a task randomly based on some randomly generated value. For instance, in a card game, developer requires to randomly shuffle the deck of cards and then distribute those cards randomly to the players. Another typical software application that makes use of random number is a lottery application where a number is randomly generated by a software application based on an algorithm and a person whose number matches with the one generated randomly wins. The above examples are just two of the thousands of application of random numbers; however these two examples are good enough to explain why generating random numbers is of such importance that Java developers have dedicated a full class to it. In the following section, the implementation and code samples of the Random class have been described.
What is Random Java Class?
In Java language, the Random class is used to generate random numbers using multiple methods. The Random class is located in java.util package and is instantiated using the “new” keyword by calling a constructor of the Random class. The Random class contains several methods that return pseudo-randomly generated integers, doubles, Boolean, bytes, floats, long and Gaussian type values.
Random Java Class: Few Examples
The first example below demonstrates how 50 random numbers between ‘0’ to ‘20’ can be generated using the the nextInt method of the Random class.
NextInt Method
Code:
import java.util.Random; public class MyClass { public static void main(String[] args) { int RandomNumbers = 50; Random num = new Random(); for ( int i=0; i<RandomNumbers; i++ ) { int randomnum = num.nextInt(20); System.out.format("Random Number: %2d, Value = %d\n", i+1, randomnum ); } } }
In the above code snippet, first the Random Java class has been imported to the application by importing java.util.Random class. In the main method of the MyClass class, an integer variable named RandomNumbers has been initialized to 50. This number represents the total number of randomly generated numbers. Next, an object of the Random class named num has been created by calling the constructor of the Random class.
A for loop has then been executed that will loop 50 times, and each time it would first call the nextInt method on the num object of the Random class. The nextInt method of the Java class takes an integer type parameter, which is the upper limit of the random number to be generated. For instance, in the above example, inside the for loop nextInt(20) method is being called. This method would return integer type value between 0 (inclusive) and 20 (exclusive). The returned randomly generated number is then printed on the console screen.
The output would contain 50 randomly generated numbers between 0 and 20. An instance of the output of the above code has been displayed here:
Random Number: 1, Value = 6
Random Number: 2, Value = 1
Random Number: 3, Value = 6
Random Number: 4, Value = 5
Random Number: 5, Value = 9
Random Number: 6, Value = 15
Random Number: 7, Value = 5
.
.
.
.
.
.
.
Random Number: 46, Value = 2
Random Number: 47, Value = 15
Random Number: 48, Value = 19
Random Number: 49, Value = 4
Random Number: 50, Value = 2
To explore more Java classes, check out a course at Udemy.com.
NextDouble and NextFloat Methods
Like nextInt method, that is used to randomly generate integers, the nextDouble and nextFloat methods are sued to randomly generate floating type and double type values between 0.0 and 1.0 (Both exclusive). The next example demonstrates how float and double values can be randomly generated via nextFloat and nextDouble methods respectively.
Code:
import java.util.Random; public class MyClass { public static void main(String[] args) { // run 20 random examples int RandomNumbers = 50; // create a new Java Random object Random num = new Random(); for ( int i=0; i<RandomNumbers; i++ ) { double randomnum = num.nextDouble(); float randomfloat = num.nextFloat(); String randval = Double.toString(randomnum); String ranfloat = Float.toString(randomfloat); System.out.println(randval +" "+ranfloat); } } }
The above example to the last one but here in this case, inside the for loop, nextDouble and nextFloat methods have been called on the Random type object num. Both of these methods return double and float respectively. These double and float type values have then been converted into strings by calling toString.
The randomly generated double and float type variables would be displayed in the output in the form of two columns, where the first column would contain randomly generated doubles values between 0.0 and 1.0 and the second column would contain float type variables between 0.0 and 1.0. The output would look something like this:
0.3251923685126039 0.39124447
0.8417402842597117 0.97791207
0.8933219245951466 0.6809344
.
.
.
.
.
.
.
.
.
.
.
.
0.00851098126473604 0.34304357
0.09477491159121731 0.19681299
0.04435399934598483 0.45021433
0.05663012451696958 0.38286084
0.9044211133780354 0.6423508
0.4395832582241389 0.0999071
The loop would execute 50, times therefore 50 such fields would appear in the output. However, the above output contains only an abridged portion of the output to elaborate the concept.
Other important methods of the Random Java class are the nextBoolean and nextLong methods which would return randomly generated Boolean and Long type values respectively.
Recommended Articles
Top courses in Java
Java students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.