Generate a random number between two terminals in java
Random numbers are used in different fields such as simulation to simulate a phenomenon such as the fall of a body or video games such as Sudoku where the grid must be different from the previous game or a draw, etc.Java has two ways to generate random numbers: The first is the java.util.Random and the second is the random(). In this tutorial, we'll see how to generate random integers located in an interval (between two bounds) in Java.
The Math.random() method in Java
The random() is simple to use, it can be found in the java.lang.Math and returns a double with a positive sign greater than 0.0 and less than 1.0. The number generated is one. double.double nb;Run:
nb = Math.random();
System.out.println(nb);
0.9610715687122661To get numbers greater than 1, multiply the result by 10,100,1000...100000.
nb=nb*100;
96.10715687122661
The java.util.Random class
This class is more powerful because it offers a generation of several pseudo-random types: integer, float, double, byte, boolean...Generate integers between zero and n in java
The next(int n) generates a number uniformly between 0 and n-1.
Random = new Random();The Random constructor creates a new number generator. Executing this code returns a digit between 0 and 8 chosen by the generator.
int nb;
nb = random.nextInt(9);
Generate integer between two terminals in java
The lower bound will be summed with the result:
nb = borne_inférieur + random(borne_superieur-borne_inférieur)
int genererInt(int borneInf, int borneSup){
Random random = new Random();
int nb;
nb = borneInf+random.nextInt(borneSup-borneInf);
return nb;
}
public static void main(String[] args) {
System.out.println("range[10, 20]: "+generate(10,20));
}
Run:
range[10, 20]:15
Generate integer using the random.nextBytes()
The nexBytes(bytes[]) returns an array of bytes random. The number of bits is equal to the size of the array passed as an argument.byte[] generate Bit(int size){Runtime:
Random random = new Random();
byte[] tbyte = new byte[size];
random.nextBytes(tbyte);
return tbyte;
}
public static void main(String[] args) {
byte[] tbyte = generateBit(20);
for(byte bit:tbyte)
System.out.print(" "+bit);
}
-8 16 -7 27 54 -44 -75 88 -91 -74 -124 10 -100 101 -96 -41 -101 71 33 74
Boolean
Boolean has two values: true or false.boolean genererBool(){
Random random = new Random();
return random.nextBoolean();
}
Generate Float and Double Numbers in java
As in the random() of the Math class, the digit is between 0.0 and 1.0.float genererFloat(){Runtime:
Random random = new Random();
return random.nextFloat();
}
double genererDouble(){
Random random = new Random();
return random.nextDouble();
}
public static void main(String[] args) {
System.out.println("float: "+generateDouble());
System.out.println("double: "+generateFloat());
}
double: 0.8538674831662335In java, the double is stored on 8 bytes and the float on 4 bytes.
float: 0.46791792
References:
Javadoc: java.until.Random class
Javadoc: Math.random() method
Generating random integers in a range with Java
java.util.Random.nextInt() Method