Generation of Random Integers in Java

Java provides us with ample options in order to generate any sort of random number that we need. This is mainly done in two ways:

  • Using the Math.random() function
  • Using the java.util.Random class

Math.random() function

The Math.random() function returns pseudorandomly chosen numbers between 0.0 and 1.0, i.e. it returns a positive double value. When the expression is encountered, it generates a new pseudorandom-number generator which is used for the calls to this function. This function is synchronized and can be used by more than one thread. However, the contention might decrease for each thread to have its own pseudorandom-number generator if the pseudorandom numbers are to be generated at a great speed. This method does not throw any exceptions.

Package: java.lang. This is a default package in Java and the programmer doesn’t have to import it to access the Math.random() function.


Using the Math.random() function to generate a random number

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/RandomNumbers/MathRandom.java
Output:-
Double no. :0.0025984574018734774
Integer no. :82
The sum is: 82.00259845740187

Using the Math.random() function to generate random numbers in a given range

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/RandomNumbers/MathRandomInARange.java
Output:-
12
20
The integer is:14

Random class in Java

The Random class can be used to generate various types of random data, eg: boolean, float, int, long, double, etc. In order to generate random numbers, an instance of the Random class needs to be created.

The Random class uses a 48-bit seed, which is modified using the linear congruential formula. The algorithms use a protected utility method that can generate up to 32 pseudorandomly generated bits on each invocation. Instances of this class are thread-safe but are not cryptographically secure.

Package: java.util.


Constructors of Random class

There are two constructors in the Random class. They are shown as follows:

  • Random(): Used to create a new random number generator.
  • Random(long seed): Used to create a new random number generator using a single long seed.

Methods of Random Class

MethodDescription
public void nextBytes(byte[] bytes) Used to generate random bytes and place them into a user-provided byte array.
public int nextInt()/public int nextInt(int bound)Returns the next pseudorandom, uniformly distributed int value from the random number generator’s sequence. If we specify the bound, the random number generated is between 0(inclusive) and bound(exclusive).
public long nextLong()Returns the next pseudorandom, uniformly distributed long value from the random number generator’s sequence.
public boolean nextBoolean()Returns the next pseudorandom, uniformly distributed boolean value from the random number generator’s sequence.
public float nextFloat()Returns the next pseudorandom, uniformly distributed float value(between 0.0 and 1.0) from the random number generator’s sequence.
public double nextDouble()Returns the next pseudorandom, uniformly distributed double value(between 0.0 and 1.0) from the random number generator’s sequence.
public double nextGaussian()Returns the next pseudorandom, Gaussian (“normally”) distributed double value with mean 0.0 and standard deviation 1.0 from the random number generator’s sequence.
public Intstream ints() Returns an effectively unlimited stream of pseudorandom int values.
public Longstream longs() Returns an effectively unlimited stream of pseudorandom long values.
public Doublestream doubles() Returns an effectively unlimited stream of pseudorandom double values.
public void setSeed(long seed)used to set the seed of the random number generator using a single long seed.

Using the Random class to generate a random number

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/RandomNumbers/RandomClass.java
Output:-
Double no. :0.6582175252636324
Integer no. :5
The sum is: 5.658217525263632

Using the Random class to generate random numbers in a given range

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/RandomNumbers/RandomClassInARange.java
Output:-
15
25
The random integer is: 17

So, that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more because learning paves way for a better understanding.

Keep Coding!! Happy Coding!! 🙂

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index