Hello everyone!!! Welcome to flower brackets blog. In this post we are going to learn random number generator java.

Getting random numbers in java is quite common these days. We can see its application in number of projects. Now let’s see how to generate random numbers using few methods,

#### math.random java example

To begin with, let’s look at math.random(). It does not take any parameters and returns a double value with a positive sign greater than or equal to 0 and less than 1.

Also read – arraylist java

Output will never be one, it may be equal to zero though that’s very very unlikely. They are chosen pseudo randomly with the uniform distribution in that range. So it’s not a perfect random generator, but it’s close.

Let’s see java program to generate random numbers,

public class RandomNumberInJava { public static void main(String[] args) { // output will be of DataType double System.out.println(Math.random()); System.out.println(Math.random()); } }

**Output:**

0.25845772465404826

0.3405985470470271

**java.util.random example**

We do this with the help of random class and you can import java.util.random. Once imported this let’s go ahead and create an instance from it.

import java.util.Random; public class JavaRandomClass { public static void main(String[] args) { Random rd = new Random(); // creating random integers int randInt = rd.nextInt(); System.out.println(randInt); // creating random doubles double randDouble = rd.nextDouble(); System.out.println(randDouble); // creating random booleans boolean randBool = rd.nextBoolean(); System.out.println(randBool); // creating random floats float randFloat = rd.nextFloat(); System.out.println(randFloat); } }

**Output:**

1786368858

0.9911121092414749

false

0.15194857

But usually when people create a random number they want it to be in certain range. So to do that let’s create a new integer that’s going to store a random integer within specific range, like this,

public static void main(String[] args) { Random rd = new Random(); // random number from 0 to 31 int num = rd.nextInt(31); System.out.println(num); // random number from 5 to 45 int num1 = rd.nextInt(46 - 5) + 5; System.out.println(num1); }

This looks exactly like the previous command, the only difference is that in between these parentheses you pick the maximum range you want your random number to go to.

So if you wanted your random number to only be able to go up to 30 then you would have to put 31. It goes up to whatever numbers in here but doesn’t include it.

Of course this also starts at 0. But sometimes you also want to start at a specific number too. So, I want to go to 5 to 45.

Now I have 0 to 45 right now. But how to start at 5? Well, imagine if above statement returns back 0, because that’s the lowest amount it can return.

If it returns back zero I want to add 5 to that so that I start at 5. To do that just see above example.

**NOTE**

Below statements doesn’t make any difference,

int num1 = 5 + rd.nextInt(46); int num1 = rd.nextInt(46) + 5;

Here’s the tricky part, though

int num1 = 5 + rd.nextInt(46);

We have to make sure it never goes above 45 and the maximum amount that this (rd.nextInt(46)) random number produces is 45.

But remember we are adding 5. So if this (rd.nextInt(46)) random number generates 45 now we are adding 5 to that which makes 50 and if it generates, say, 44 again we are adding 5 to that to get 49 and again that’s bigger.

So what we need to do is subtract our starting number, like this

int num1 = rd.nextInt(46 – 5) + 5;

That way the maximum amount this is going to make is now going to be 41 and again 41 is not included. So if this rd.nextInt(46 – 5) produces 40 which is max this can produce adding 5 to that is only going to go to 45 which is exactly what we want.

#### java.util.concurrent.ThreadLocalRandom to generate random numbers

We are going to input the package called java.util.concurrency.ThreadLocalRandom. ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified.

Use of ThreadLocalRandom is particularly appropriate when multiple tasks use random numbers in parallel in thread pools.

import java.util.concurrent.ThreadLocalRandom; public class ThreadLocalRandomDemo { public static void main(String[] args) { // random integers int randomInteger = ThreadLocalRandom.current().nextInt(); System.out.println(randomInteger); // random doubles double randomDouble = ThreadLocalRandom.current().nextDouble(); System.out.println(randomDouble); // random booleans boolean randomBoolean = ThreadLocalRandom.current().nextBoolean(); System.out.println(randomBoolean); } }

**Output:**

951227305

0.9477534236023137

false