If you are a software developer, you may have come across the requirement of generating random numbers in your application. While developing any kind of applications, you often need to generate random numbers. In Java, it is easy to generate random numbers using some in-built methods and classes. In this article, we will see three different ways to generate random numbers in java. Let’s start with the definition of random numbers.
What Is Random Number?
“Randomness” is something which is totally unpredictable and unbiased. Random number is a number picked randomly from the given set of numbers. There will be no relation or dependency between the two successively picked numbers.
Random Number Generator :
An ideal Random Number Generator is the one which generates a series of numbers in the given range where,
Each possible number have equal probability of picking up.
There will be no relation or dependency between the previously generated number and the present one.
How To Generate Random Numbers In Java?
Java provides two ways to generate random numbers. One is using java.util.Random class and another one is using Math.random() method. There is one more method introduced in JAVA 7. It is using ThreadLocalRandom class. We will discuss all three methods in this article.
Generating Random Numbers Using java.util.Random Class :
Using java.util.Random class, you can generate random integers, doubles, floats, longs and booleans. Below is the program which generates random integers, doubles and booleans using java.util.Random class.
What Is Random Number?
“Randomness” is something which is totally unpredictable and unbiased. Random number is a number picked randomly from the given set of numbers. There will be no relation or dependency between the two successively picked numbers.
Random Number Generator :
An ideal Random Number Generator is the one which generates a series of numbers in the given range where,
Each possible number have equal probability of picking up.
There will be no relation or dependency between the previously generated number and the present one.
How To Generate Random Numbers In Java?
Java provides two ways to generate random numbers. One is using java.util.Random class and another one is using Math.random() method. There is one more method introduced in JAVA 7. It is using ThreadLocalRandom class. We will discuss all three methods in this article.
Generating Random Numbers Using java.util.Random Class :
Using java.util.Random class, you can generate random integers, doubles, floats, longs and booleans. Below is the program which generates random integers, doubles and booleans using java.util.Random class.
Output :
Random Integers : 238886529
Random Integers : 1463682739
Random Integers : -1247541726
Random Integers : 34314299
Random Integers : 1658793122
—————————–
Random Doubles : 0.7443798462870127
Random Doubles : 0.8382139571324086
Random Doubles : 0.9680505961503302
Random Doubles : 0.5623901155047567
Random Doubles : 0.37285000690287773
—————————–
Random booleans : false
Random booleans : true
Random booleans : false
Random booleans : false
Random booleans : false
Note : Integers generated are in the range of -231 and 231-1. Generated doubles are in the range from 0.0 to 1.0.
Generating Random Numbers Using Math.random() :
Output :
Random Doubles : 0.7783847001873636
Random Doubles : 0.3895881606319488
Random Doubles : 0.4798586710671108
Random Doubles : 0.6203717760219122
Random Doubles : 0.7460629328768049
Generating Random Numbers Using ThreadLocalRandom Class :
java.util.concurrent.ThreadLocalRandom class is introduced in Java 7. Below program shows how to generate random integers, doubles and booleans using ThreadLocalRandom class.
Output :
Random Integers : -1626309835
Random Integers : 332111237
Random Integers : -1585909207
Random Integers : 1070972416
Random Integers : -1874591696
—————————–
Random Doubles : 0.7840295282815645
Random Doubles : 0.36849802111866514
Random Doubles : 0.6551894273753474
Random Doubles : 0.5993075213578543
Random Doubles : 0.7289343843664791
—————————–
Random booleans : true
Random booleans : false
Random booleans : false
Random booleans : true
Random booleans : true
Generating Random Numbers In The Given Range :
Below is the java program which generates random integers in the range from 0 to 50 using all three methods – Random class, Math.random() and ThreadLocalRandom class.
Output :
Random integers between 0 and 50 using Random class : 16 12 30 26 17
Random integers between 0 and 50 using Math.random() : 12 43 42 32 45
Random integers between 0 and 50 using ThreadLocalRandom 12 40 16 17 3
No comments:
Post a Comment