Scanner class Methods in Java

Generating Random Number in Java

 Random Numbers

At many places we require our program to generate random numbers.Like, you want to make a program where you can roll a dice there you will use random number generator for 6 numbers.Random numbers are very helpful in many things. Another example is to change the background to random colors. 

Today, we will learn how to generate Random numbers in java.There are two methods to generate random numbers:-

  1.  Math.random() method
  2. Random class

Random numbers using Math.random()

In java, Math class contains a lot useful methods.The random() method is one of them.It helps a lot with generating random numbers. You do not need to import java.Math class for using this method.Instead you can statically import it.

Example:

Math.random()

You can use method syntax as above. To print new random number every time on screen,enclose this method in print statement.

Example: 

System.out.println(Math.random());

Each time you run this code, it will print random number between 0 and 1.

How to generate random numbers till N number using random() method?

To generate random number in the range of N numbers.We only need to multiply random method with that N number.If you want to print random number till 6 then multiply with 6.

Example:

Write a program to generate random number in the range of  20.This program will print 6 random numbers on screen using same code for random numbers.


Random number generator in java example
Generating random numbers in the range of 20
In the above,
  1. We are using a simple print statement to print numbers on screen.
  2. The random numbers are printed using Math.random()*20, method.The random() method is multiplied by 20 so it prints numbers from 0 to 20.You can replace 20 with any number.
Note: By default, random numbers are of double type.You can explicitly convert them to int type.Example: int a=(int)(Math.random()*20); System.out.println(a);

Output:

Random number generator in java example
Output of Random Numbers

Random numbers using Random Class

Java provide us a Random class to work with random numbers.This class contains helpful methods for  generating random numbers of desired type like, int , float, double, long etc.

We can also generate bounded numbers as  numbers within the range of N number.

Methods provided by Random class:

  1. nextInt()
  2. nextInt(n) //bounded method
  3. nextFloat()
  4. nextLong()
  5. nextDouble()
  6. nextBoolean

Example: This example will illustrate all these methods and will print their result on screen.

Write a program to generate random numbers using Random class.


Write a program to generate random numbers using Random class.
Generate random numbers using Random class.


In the above example,to use Random class you need follow these steps,
  1. Import java.util.Random class.
  2. Create an object of Random class as we created "randomNumberGen" object of Random class.
  3. To print using a variable,define an int variable like "randomNumber" in the example. Assign it to the method call .We are calling nextInt(50) method using "randomNumberGen" Object.This method will generate number withing range of 50.
  4. Analyse other print statements to understand their working.
Output:

Write a program to generate random numbers using Random class.

Experiment: Try generating random numbers till 100 and 1000. 

Thanks for reading.Happy Coding!

Comments

Post a Comment

If you have any doubt, ask here