chevron_left
2,504 points
20 16 12

There are times when it is necessary to use random numbers and in Java we have 2 easy ways to obtain it either with Math.random or with java.util.Random which offers us some more possibilities although its use is just as simple.

MATH.RANDOM ():

If you choose to use the random function of the Math class, the process is very simple since this function returns a random number between 0 and 1 (double type) and it is only necessary to multiply it by the number that we want the number to be highest possible and / or add (or subtract) a number so that the lower limit is something other than 0.

This function is static so there is no need to instantiate an object of the Math class.

int number = (int) (Math.random () * 10 + 1);

In this case, the generated number will be between 1 and 10, because first we multiply it by 10 with what we would have a number between 0 and 9 and by adding 1 to this number we will finally have a number between 1 and 10.

From this example we get that the generated number will be between 0 and the number by which we multiply without including it and to modify the lower limit we must add or subtract to obtain the desired lower limit, for example if we want to obtain numbers between 25 and 75 we would multiply times 51 (and we would get numbers between 0 and 25) and add 25 to it and we already have a random number between the margins that we wanted. The formula to obtain a number between any two numbers is (X-Y + 1) + Y.

int number = (int) (Math.random () * (X-Y + 1) + Y;
int number = (int) (Math.random () * (75-25 + 1) +25);

And to finish this case, a typical example of random numbers is the lottery and with this function and a loop we can generate a possible combination, we are going to generate a combination for the euromillions and as in the lottery the numbers are not repeated, we must control it because being random numbers they could be repeated.

int number;
ArrayList numbers = new ArrayList ();
ArrayList stars = new ArrayList ();

// Generate 5 numbers between 1 and 50
for (int i = 1; i <= 5; i ++) {
     number = (int) (Math.random () * 50 + 1);
     if (numbers.contains (number)) {
         i--;
     } else {
         numbers.add (number);
     }
}
System.out.println ("The EuroMillions combination is:");
for (Integer n: numbers) {
     System.out.println (n + "");
}
// Generate 2 numbers between 1 and 11
for (int i = 1; i <= 2; i ++) {
     number = (int) (Math.random () * 11 + 1);
     if (stars.contains (number)) {
         i--;
     } else {
         stars.add (number);
     }
}
System.out.println ("\ nAnd the stars are:");
for (Integer n: stars) {
     System.out.println (n);
}

JAVA.UTIL.RANDOM:

The dynamics to generate the random numbers is the same as that of the previous method, but it offers a greater range of possibilities regarding the type of the generated numbers (boolean, integer, double, float, Long and byte arrays), in the form in the one that generates the numbers (Gaussian distribution) and it is also allowed to pass a seed to it to generate the numbers from this although it must be taken into account that if it is a fixed number the numbers will always be generated following the same sequence so if 2 instances are created 2 with the same seed both will produce the same sequence. If no seed is indicated by default, System.currentTimeMillis (); is used.

Random randomNum = new Random ();
// Constructor pass it a seed
Random randomNum1 = new Random (234);

// Integer between 25 and 75
int n = randomNum.nextInt (75-25 + 1) + 25;

// Assign a new seed
randomNum.setSeed (1235);

// Decimal number between 0.0 and 1.0
double d = randomNum.nextDouble ();

 

In this case, it is necessary to create an instance of the Random class and then the random number is generated with the nextXXX () methods with the Random class object previously instantiated.

And to test the use of java.util.Random we are going to see a simple example to simulate that we are flipping a coin:

Random randomNum = new Random ();
// Constructor pass it a seed
Random randomNum1 = new Random (234);

// Integer between 25 and 75
int n = randomNum.nextInt (75-25 + 1) + 25;

// Assign a new seed
randomNum.setSeed (1235);

// Decimal number between 0.0 and 1.0
double d = randomNum.nextDouble ();

With this we are ready to generate the random numbers that we need for anything that occurs to us and if we are going to generate random numbers and we do not want future results to be predicted (or make it more difficult) we can use the SecureRandom class that for use purposes it is the same as Random although internally it is different and does not use the system time to generate the random numbers but other more complex forms.

More Posts

How to get a random number between 1 and 10 in java. Huzaifa-Glitch - May 22
Java random number between 1 and 100 Hasnain_khan - Oct 9, 2020
Creating Random alphanumeric String in java yoniortho - May 5
Generate random number in java between 1 and 10 Hafsa_Rao - Sep 13
How to generate random numbers in java within range Hasnain_khan - Oct 13, 2020
Generate random numbers between 1 and 10 in C++ Lalit Kumar - May 12, 2020
Generate random numbers between 1 and 100 in C++ Lalit Kumar - May 12, 2020
How to Generate Random Number in Java muliemes - May 17
Javascript random number between 1 and 50 amna - Oct 18, 2020
choose a random number in java SharadMagar450 - Aug 7