Random Number Generator Java

Hello everyone!!! Let me explain random number generator java.

Well, there are many ways in java to generate random numbers.

Also Read – Java Program To Find Prime Number

Example: random number generator java

In the following example we are going to demonstrate java random number generator using for loop.

```import java.util.Scanner;
import java.util.Random;

public class RandomNumbers
{
public static void main(String[] args)
{
int a;
int range;
int randomNumber;

Random rd = new Random();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter random numbers you want to generate: ");
range = sc.nextInt();

// java random number generator between range 0 - 100

for(a = 0;a <= range; a++)
{
randomNumber = rd.nextInt(100);
System.out.print(randomNumber + " ");
}
}
}

```

Output:

Java random using math.random()

Let’s see an example on how to generate random number java using math.random() method.

Math class has many methods for executing numeric calculations and random() method is one of them.

This method returns a double value which is greater than or equal to zero or less than one.

```public class JavaRandom
{
public static void main(String[] args)
{
// generate random number java
// returns a double value
System.out.println("Random number java doubles : " + Math.random());
System.out.println("Random number java doubles : " + Math.random());
}
}

```

Output:

Random number java doubles : 0.10832457378843408
Random number java doubles : 0.827561704322581

Using Math.random() method and scanner class

Now here’s java random number generator using math.random() method and scanner class,

```import java.util.Scanner;

public class RandomGeneratorJava
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter minimum number : ");
int mini = sc.nextInt();

System.out.println("Please enter maximum number : ");
int max = sc.nextInt();

int randomNum = mini + (int)(Math.random() * max);

System.out.println("Random integer java : " + randomNum);
sc.close();
}
}

```

Output:

Please enter minimum number : 1
Please enter maximum number : 100
Random integer java : 26

Let’s see an example on how to generate a random numbers in java,

```import java.util.concurrent.ThreadLocalRandom;

public class RandomJava
{
public static void main(String[] args)
{
// random integer generator java
System.out.println("Java Random Int : " + randomInteger);

// random doubles
System.out.println("Java Random Doubles : " + randomDouble);

// random booleans
System.out.println("Java Random Booleans : " + randomBoolean);
}
}

```

Output:

Java Random Int : -494216187
Java Random Doubles : 0.356150792349613
Java Random Booleans : true

Java random number within specific range

Here we are going to learn java random number generator within specific range. We are using import java.util.random class.

Here’s the example for java random,

```import java.util.Random;

public class RandomNumberJava
{
public static void main(String[] args)
{
System.out.println("Java generating random number in range 1 to 100 : ");
int numStart = 1;
int numEnd = 100;
Random rd = new Random();
for(int a = 1; a <= 100; ++a)
{
findRandom(numStart, numEnd, rd);
}
}

private static void findRandom(int numBegin, int numStop, Random rand)
{
if(numBegin > numStop)
{
throw new IllegalArgumentException("Begin should not be more than Stop!!");
}
// java random range
long range = (long)numStop - (long)numBegin + 1;
long num = (long)(range * rand.nextDouble());
int randomNumber = (int)(num + numBegin);
System.out.print(" " + randomNumber);
}
}

```

Output:

Java generating random number in range 1 to 100 :
43 29 34 37 30 87 37 8 63 34 41 30 24 50 61 41 56 100 13 37 94 55 89 44 22 87 27 39 57 13 74 82 5 82 41 3 6 25 83 17 43 60 13 19 4 76 20 11 62 47 60 88 15 86 76 64 61 85 68 15 4 8 87 1 40 51 4 69 99 51 10 33 69 80 85 15 90 84 38 59 2 3 6 28 15 54 21 82 72 88 44 42 55 88 58 62 32 43 73 6

Method 1: java.util.Random class

In this method let’s learn how to generate random numbers within a range in java.

In the example below we are using java.util.Random class. This class provides a method nextInt which generates random numbers from zero to given value.

nextInt method takes argument of type integer.

```import java.util.Random;

public class RandomIntJava
{
public static void main(String[] args)
{
Random randomJava = new Random();
int randomInt = randomJava.nextInt(50) + 1;
System.out.println("Random number generator java : " + randomInt);
}
}

```

Output:

Random number generator java : 37

Method 2 : generate random number java using ThreadLocalRandom class

Here’s an example on how to generate unique random numbers in java using ThreadLocalRandom class.

```import java.util.concurrent.ThreadLocalRandom;

public class JavaRandomInt
{
public static void main(String[] args)
{
System.out.println("Random number generator java : " + randomJava);
}
}

```

Output:

Random number generator java : 40

Random Number Generator Java – normal distribution

Let’s see an example on how to use the random class in java

```import java.util.Random;

public class JavaRandomClass
{
private Random rd = new Random();

public static void main(String[] args)
{
JavaRandomClass jrc = new JavaRandomClass();
double mean = 100.0f;
double variance = 5.0f;
for(int a = 1; a <= 10; ++a)
{
System.out.print("Random numbers java : " + jrc.calculateGaussian(mean, variance) + "\n");
}
}

private double calculateGaussian(double numMean, double numVariance)
{
return numMean + rd.nextGaussian() * numVariance;
}
}

```

Output:

Random numbers java : 98.35815775440709
Random numbers java : 99.28575088195106
Random numbers java : 97.13481928944009
Random numbers java : 98.91614002254799
Random numbers java : 102.34850302242776
Random numbers java : 95.95433799479389
Random numbers java : 102.29052805377971
Random numbers java : 93.82563142560224
Random numbers java : 103.80336905969705
Random numbers java : 100.38896826829429

Random in java using random class

Here’s unique random number generator java using random class. After creating object of random class we need to call nextInt() method.

```import java.util.Random;
import java.util.Scanner;

public class RandomInJava
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter minimum number : ");
int mini = sc.nextInt();

System.out.println("Please enter maximum number : ");
int max = sc.nextInt();

Random rd = new Random();
int range = max - mini + 1;
int randomNumber = rd.nextInt(range) + mini;

System.out.println("Random integer java : " + randomNumber);
sc.close();
}
}

```

Output:

Please enter minimum number : 1
Please enter maximum number : 100
Random integer java : 16

Generate unique random numbers java

Let’s learn how to generate unique random numbers in java with an example,

```import java.util.ArrayList;
import java.util.Collections;

public class UniqueRandomNumbers
{
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>();
for(int a = 1; a < 10; a++)
{
}
Collections.shuffle(al);
for(int b = 0; b < 4; b++)
{
System.out.println("Random integer java : " + (al.get(b)));
}
}
}

```

Output:

Random integer java : 9
Random integer java : 8
Random integer java : 6
Random integer java : 2

Well, now let’s see how to generate random numbers in java without repetitions with an example,

```import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class RandomJavaExample
{
public static final int SIZE_REQUIRED = 20;
public static final int NUMBER_RANGE = 100;

public static void main(String[] args)
{
Random rd = new Random();

Set<Integer> set = new HashSet<Integer>(SIZE_REQUIRED);

while(set.size()< SIZE_REQUIRED)
{
}
assert set.size() == SIZE_REQUIRED;
System.out.println("Random integer java : " + set);
}
}

```

Output:

Random integer java : [0, 64, 34, 3, 68, 5, 70, 39, 43, 77, 47, 16, 20, 21, 55, 26, 91, 61, 29, 95]

conclusion

So this was all about random number generator in java and how to generate unique random numbers in java.

I hope you have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.