Class Random. Generating of random numbers. Class constructors. List of methods. Methods for obtaining single random numbers
Contents
- 1. Class Random. General concepts. Class constructors
- 2. Base methods of class Random. The list
- 3. Method setSeed(int). Set initial value
- 4. Method nextBoolean(). Get random boolean value
- 5. Method nextDouble(). Get a random number of double type. Formation of numbers within specified limits
- 6. Method nextFloat(). Get a random number of type float
- 7. Method nextGaussian(). Get pseudo-random value based on Gaussian distribution
- 8. Method nextInt(). Get a random number of type int. Get an integer within the given limits
- 9. Method nextLong(). Get a random number of type long
- Related topics
Search on other Web-resources:
1. Class Random. General concepts. Class constructors
The Random class is used to generate random numbers. To use the capabilities (methods) of the Random class at the beginning of the application, you need to include the java.util.Random module as follows
import java.util.Random;
The class constructors create a new random number generator. The following main constructors are used to create a generator
Random()
Random(int seed)
where
- seed – the initial number against which the random number sequence is generated. This number is supported by the next() method, which generates a pseudo-random number (pseudorandom number).
Creating instances of the Random class that will generate sequences of random numbers using the above constructors could be something like this
Random rnd1 = new Random(122); Random rnd2 = new Random();
where
- r1, r2 – instances that will generate sequences of random numbers;
- 122 – an integer from which the formation of the next random numbers begins. Here you can set any other integer (55, 101… etc.).
After creating an instance of the Random class, methods are directly available that allow you to get sequences of random numbers.
⇑
2. Base methods of class Random. The list
The Random class contains a number of methods for generating random numbers:
- setSeed() – sets the initial value, which is used as the basis for generating random numbers;
- doubles() – generates data of type double as a stream of random numbers;
- ints() – generates data of type int as a stream of random numbers;
- longs() – generates a data stream of type long containing random numbers;
- nextBoolean() – returns a random value of type boolean;
- nextBytes() – returns an array of random numbers of type byte[];
- nextDouble() – returns a random number of double type;
- nextFloat() – returns a random number of float type;
- nextGaussian() – returns a pseudo-random value based on the Gaussian distribution;
- nextInt() – returns a random number of type int;
- nextLong() – returns a random number of type long.
⇑
3. Method setSeed(int). Set initial value
The setSeed() method allows you to set the initial value, on the basis of which a sequence of random numbers is generated. In the sequence, each subsequent number depends on the previous one. According to the documentation, the signature of the setSeed() method is as follows
void setSeed(long seed);
where
- seed – initial value, on the basis of which a sequence of random numbers is generated.
Creating an instance of the Random class using a constructor with a parameter
Random rnd = new Random(seed);
is equivalent to the strings
Random rnd = new Random();
rnd.setSeed(seed);
where
- seed – some integer value of type long.
Example.
The example creates a stream of data containing 10 random numbers of the double type. The initial value (seed) is first set to 122, then it is overridden with a new value of 150 using the setSeed() method.
import java.util.Iterator; // needed to use the Iterator<> class import java.util.Random; // needed to use the Random class import java.util.stream.DoubleStream; // needed to use the DoubleStream class public class MathFunctions { public static void main(String[] args) { // 1. Create thread based on seed 122 Random r1 = new Random(122); // 2. Install a new seed - calling the setSeed() method r1.setSeed(150); // 3. Get a stream of 10 doubles DoubleStream ds1 = r1.doubles(10); // 4. Get an iterator on the stream ds1 Iterator<Double> it1 = ds1.iterator(); // 5. Output 10 random numbers in a loop from the stream ds1 System.out.println("Stream ds1:"); while (it1.hasNext()) { System.out.println(it1.next()); } } }
Result
Stream ds1: 0.7436883394251182 0.16523214952520537 0.6551835693241186 0.46667154390807386 0.02763677886761373 0.7815907963879736 0.23597049719139052 0.3714539686578885 0.22142228403292508 0.5329685061913043
⇑
4. Method nextBoolean(). Get random boolean value
The nextBoolean() method allows you to get a random boolean value. According to the documentation of the method declaration in the Random class the following
public boolean nextBoolean();
Example.
import java.util.Random; public class MathFunctions { public static void main(String[] args) { // 1. Create a stream based on an arbitrary initial value Random r1 = new Random(); // 2. Get a random number of boolean type boolean b1 = r1.nextBoolean(); System.out.println("b1 = " + b1); } }
Result
b1 = true
⇑
5. Method nextDouble(). Get a random number of double type. Formation of numbers within specified limits
The nextDouble() method returns a random number of double type. The value of the number lies within [0.0; 1.0[ – value 0.0 inclusive, value 1.0 exclusive. According to the documentation, the method declaration is as follows
public double nextDouble();
Example.
In the example, an array of 10 double numbers is first formed. This array is then displayed on the screen. The value of each double is within [min; max) where min and max are entered from the keyboard.
import java.util.Random; // needed to use the Random class import java.util.Scanner; // needed to use the class Scanner public class MathFunctions { public static void main(String[] args) { // 1. Create a stream based on an arbitrary initial value Random rnd = new Random(); // 2. Create the array of 10 double elements double[] AD = new double[10]; // 3. Set the lower and upper bounds of random numbers from the keyboard double min, max; Scanner input = new Scanner(System.in); // 3.1. Input the lower bound System.out.print("min = "); min = input.nextDouble(); // 3.2. Input the upper bound System.out.print("max = "); max = input.nextDouble(); // 4. Fill the AD array with random numbers within [min; max] double value; for (int i=0; i<AD.length; i++) { // Get a random number within [0.0; 1.0) value = rnd.nextDouble(); // Correct the limits of value as [min; max] AD[i] = min + value * (max - min); } // 5. Display the array AD System.out.println("\nAD:"); for (int i=0; i<10; i++) System.out.println(AD[i]); } }
Result
min = 2.5 max = 3.8 AD: 2.8359371330191094 2.758080086113864 3.573990757948374 3.3258831628588483 3.4857770403575628 3.659913452046651 3.218060124836948 3.032705795552893 3.7447915830146092 2.791155939366058
⇑
6. Method nextFloat(). Get a random number of type float
The nextFloat() method returns a random float number. The method signature is as follows
float nextFloat();
The method returns a number that is in the range [0.0f; 1.0f).
Example.
The example forms the array of 10 random float numbers. The numbers are in the range [-5; 5).
import java.util.Random; // needed to use the Random class import java.util.Scanner; // needed to use the class Scanner public class MathFunctions { public static void main(String[] args) { // 1. Create a stream based on an arbitrary initial value Random rnd = new Random(); // 2. Create the array with 10 float elements float[] AF = new float[10]; // 3. Set the lower and upper limit of random numbers from the keyboard float min, max; Scanner input = new Scanner(System.in); // 3.1. Input the lower bound System.out.print("min = "); min = input.nextFloat(); // 3.2. Input the upper bound System.out.print("max = "); max = input.nextFloat(); // 4. Fill array AF with random numbers within [min; max] float value; for (int i=0; i<AF.length; i++) { // Get a random number within [0.0; 1.0) value = rnd.nextFloat(); // Correct the limits of value as [min; max] AF[i] = min + value * (max - min); } // 5. Display the array AF System.out.println("\nAF:"); for (int i=0; i<10; i++) System.out.println(AF[i]); } }
Result
min = -5 max = 5 AF: -0.4368925 -4.857518 -1.9285429 -1.3483305 -1.2607784 3.6692915 3.8821516 4.8362236 3.9554806 0.30514526
⇑
7. Method nextGaussian(). Get pseudo-random value based on Gaussian distribution
The nextGaussian() method returns a double value based on a Gaussian distribution with a mean of 0.0 and a standard deviation of 1.0. According to the documentation of the method declaration in the Random class the following
public double nextGaussian();
Example.
import java.util.Random; public class MathFunctions { public static void main(String[] args) { // 1. Create a stream based on the arbitrary initial value Random rnd = new Random(); // 2. Create the AD array of 10 double elements // in which the elements are obtained based on the Gaussian distribution double[] AD = new double[10]; // 3. Fill AD array with random numbers using nextGaussian() method for (int i=0; i<AD.length; i++) AD[i] = rnd.nextGaussian(); // 4. Display the AD array on the screen System.out.println("Array AD:"); for (int i=0; i<AD.length; i++) System.out.println(AD[i]+" "); } }
Result
Array AD: 0.8462285437123548 1.1084379848996055 0.5552376880883958 -1.0787586788543444 0.9944069427425211 0.8645907102307332 -1.449023177851094 1.2567221522577043 -1.073685704004384 -0.8709705603926711
⇑
8. Method nextInt(). Get a random number of type int. Get an integer within the given limits
The nextInt() method returns a random integer of type int. The number can be either positive or negative. According to the documentation, the method declaration is as follows
public int nextInt();
Example.
In the example, using the nextInt() method, an array of random numbers is formed. Each number has a value within [min; max]. To form the number, a formula is derived. At the end, the array of generated numbers is displayed. To input numbers from the keyboard, the tools of the Scanner class are used.
import java.util.Random; // needed to use the Random class import java.util.Scanner; // needed to use Scanner class public class MathFunctions { public static void main(String[] args) { // 1. Create a stream based on an arbitrary initial value Random rnd = new Random(); // 2. Create the array of 10 int elements int[] AI = new int[10]; // 3. Set the lower and upper limit of random numbers from the keyboard int min, max; Scanner input = new Scanner(System.in); // 3.1. Input the lower bound System.out.print("min = "); min = input.nextInt(); // 3.2. Input the upper bound System.out.print("max = "); max = input.nextInt(); // 4. Fill AI array with random numbers within [min; max] for (int i=0; i<AI.length; i++) AI[i] = min + Math.abs(rnd.nextInt()) % (max - min + 1); // 5. Display the array System.out.println("\nAI:"); for (int i=0; i<10; i++) System.out.println(AI[i]); } }
Result
min = 1 max = 10 AI: 7 10 5 5 8 2 1 4 9 5
⇑
9. Method nextLong(). Get a random number of type long
The nextLong() method returns a random number of type long. According to the Java documentation, the method declaration looks like this
public long nextLong();
Example.
import java.util.Random; public class MathFunctions { public static void main(String[] args) { // 1. Create a stream based on an arbitrary initial value Random rnd = new Random(); // 2. Create an array AL of 10 elements of type long long[] AL = new long[10]; // 3. Fill AL array with random numbers using nextLong() method for (int i=0; i<AL.length; i++) AL[i] = rnd.nextLong(); // 4. Display array AL on the screen System.out.println("Array AL:"); for (int i=0; i<AL.length; i++) System.out.println(AL[i]+" "); } }
Result
Array AL: 7366919727266127728 -979699283376638676 5249017576319962809 -4343155317134154076 6027817083452027820 -2862223376222694775 1510496055653313188 5133621007379261861 5000401929540016138 5865861847345821978
⇑
Related topics
⇑