Copyright

Java: Generate Random Number Between 1 & 100

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: How to Square a Number in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Random Number Generation
  • 2:01 A Note on Randomness
  • 3:12 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Random numbers are important in programming, especially in gaming. This lesson will describe how to generate a random number between 1 and 100. We will also discuss the concept of randomness.

Random Number Generation

Java includes a wealth of functions that you can use in your programs. However, Java runs with a minimal amount of functions already available. This helps with processing time and saves extra lines of code. When you want to use a certain feature, such as receiving input from the user or generating a random number, you need to import that utility into your code. We use the import statement to do this. The tool we'll be using is the Random class, which is part of Java's utility (util) library. To do this, you need to have this line of code at the beginning of our program, before the first class or main function:

import java.util.Random;

We will need to set our limits; in this case we only want a number between 1 and 100. Let's create two integer variables for this purpose, like this:

// what is our range?
int max = 100;
int min = 1;

Next we can setup the actual random number generator. Random is a class in Java, with its own methods. We can create an instance of this class and use all of these cool features. In order to create a new instance of Random, this code is used:

// what is our range?
int max = 100;
int min = 1;
// create instance of Random class
Random randomNum = new Random();

Finally, we can generate our random number. The Random class provides a method called nextInt(int n), which generates a random number between 0 and the number specified (n). We want only numbers between 1 and 100, so the code is tweaked a little bit. It may look a little confusing, but we'll walk through it. Here is the code to generate a random number between 1 and 100 and save it to a new integer, showMe:

int showMe = min + randomNum.nextInt(max);

Let's start at the beginning (after the equal sign). We need to make sure we include 1, for the minimum value, as laid out in the requirements. This is why it's sitting in front of the nextInt function. Next, we call the nextInt function and pass it the maximum value. In this case it's 100.

Each time we run the program a different number is displayed between 1 and 100. Here is the final, complete code:

public static void main(String[] args) {
  // what is our range?
  int max = 100;
  int min = 1;
// create instance of Random class
  Random randomNum = new Random();
  int showMe = min + randomNum.nextInt(max);
  System.out.println(showMe);
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support