Copyright

Java Random: Method & Examples

Instructor: Sergey Segal

Sergey has a Masters in Biomedical Engineering and has taught science and mathematics courses at the University of Wisconsin-Madison.

This lesson will teach you how to generate random numbers in Java. We will look at an example of calculating the revenue generated by an airline on a hypothetical flight from Chicago to New York.

Introduction to the Java Random Method

Random numbers have a variety of applications in the engineering and science fields. Although you cannot generate truly random numbers using a programmable algorithm, there exist built-in classes and functions to generate numbers that appear to be random. In this lesson, you will learn about how to use the random() method in Java, which generates pseudo-random decimal values, x, in the range 0.0 ≤ x < 1.0. This method is invoked by the statement Math.random(). Note that the generated numbers can have a maximum value of 0.999999...

It's also worth mentioning that there is a class Random() that can be imported via java.util.Random, but the Random() class is outside the scope of this lesson.

Let's proceed to implement the random() method in an example.

Java Random Method Example

Suppose you are working in the analytics department of an airline company and would like to get an idea of the potential revenue for a flight from Chicago to New York. Suppose there are three ticket price categories: economy, business, and first class. We denote each of these categories with integer values as follows:

Economy Class: 3

Business Class: 2

First Class: 1

The plane capacity is 200: 25 first class seats, 25 business class seats, and 150 economy class seats. The price for each seating category is as follows:

Economy Class: $100

Business Class: $170

First Class: $250

That is, we assume that the price for each class can only take on one value: $100 for flying economy class, $170 for flying business class, and $250 for flying first class. Further, suppose that if more than 25 people per category wish to buy either first class or business class seats, each additional person beyond the 25-seat maximum capacity will opt not to downgrade and bring no revenue.

We first initialize the variables:

double r = 0.0;
int priceCategory = 0;
int revenue = 0;
int firstClassPassengerCount = 0;
int businessClassPassengerCount = 0;

Then generate pseudo-random integers between 1 and 3, inclusive, for the price categories:

r = Math.random();
priceCategory = (int)(3.0*r) + 1;

Note how we first use Math.random() to generate a decimal value between 0.0 and 0.99999..., inclusive, then multiply this decimal value by 3 to convert it to the range between 0.0 ≤ r < 3.0, with the largest possible value being 2.99999... We then truncate the result with (int)(...), resulting in integer values between 0 and 2, inclusive, and add a 1 to generate integer values that are in the set {1, 2, 3}.

Subsequently, we code the conditional statements to calculate the revenue:

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