Chapter 6. Helping Bud. Random Numbers
I have news from my lawyer and it’s not good at all. My computer was used to access a criminal website that provided services to hack companies’ security systems. But that’s not all, this software is currently being used to extort several multinational companies.
The police believe that I devised the plan along with an accomplice who is still at large. I guess they think it’s Chani. I hope they catch him so this whole matter can be cleared up. Meanwhile, defending myself against the accusations is very difficult. My computer was involved and few people believe it was used without my permission.
Rich’s statements are the only ones that can help me. He has come to visit me a couple of times and told me that he has spoken several times with the police to explain to them how my laptop ended up in Chani’s hands. He even told them that in our house we didn’t lock the doors and we left our electronic devices anywhere.

The truth is I’m not very optimistic. I imagine Chani must have fled far away. If they don’t arrest him, I’m going to spend quite a few years here. Again, I think about how life changes in an instant. I haven’t done absolutely anything and now I have to pay for a crime I have nothing to do with. The worst part is that I can’t do anything. I can only wait to see what happens. My social circle has been drastically reduced; before, I used to fill the house with friends on weekends. Now, I practically only talk to Bud and once a day with the visitors I receive from the outside. My family is very worried and I know they’re trying to help me from the outside. I don’t feel alone, but sometimes I miss having more company.
At least I have Bud; we spend almost all day talking. Most of the time, about nonsense, nothing serious. It’s better to keep a clear head and try to disconnect a bit. Sometimes he asks about my situation and follows the developments, but the truth is that not even I know what’s happening.
Bud is lucky; he can spend much of the day with the computer. He can even leave the cell on many occasions to help prison employees with various computer tasks. Perhaps, if I learn, I could also lend a hand. It could be a good opportunity to learn things, entertain myself, and, in the process, see if they can reduce my sentence.

Bud loves to teach and constantly asks me surprise questions. He told me that if I apply myself, he might give me a small job or at least let me help him. We have a deal. He’s going to teach me how to work with random numbers in Java and then give me a little test. If I pass it, he’ll let me help him.
Working with Random Numbers
Importance of Random Numbers in Java
Random numbers are constantly used in all types of programs. They can serve us, among other things, to simulate random events in games and simulations, generate data for software testing, and optimize algorithms through random decisions.
Furthermore, they are vital in computer security for creating keys and in data analysis for modeling statistical distributions. In summary, random numbers in Java are versatile tools that improve the robustness, security, and performance of computer systems in various applications.
Generating Random Numbers with Math.random
First of all, you should know that there isn’t just one way to generate random numbers. I’m going to show you what, in my view, is the simplest method. In Java, random number generation can be done using the Math class and its static random() method. The number we obtain is greater than or equal to 0.0 and less than 1.0. This means it can generate numbers like 0.0, but never reaches 1.0 (that is, 0.999999999… is the maximum possible value).
As you can see, we have some limitations, but nothing we can’t solve with a bit of imagination and creativity.
Now, let’s see how to use Math.random() in practice to generate random numbers in Java:
- Generate a random number between 0.0 (inclusive) and 1.0 (exclusive)
double number = Math.random();
System.out.println("Random number generated: " + number);
- Generate a random number between two figures. Using decimals.
For this, we use the following structure:
double number = Math.random() * (max - min) + min;
For example, if we want to create a random number between 10.0 (Included) and 100.0 (Excluded), we’ll do it like this:
double number = Math.random() * (100 - 10) + 10;
System.out.println("Random number between 10.0 and 100.0: " + number);
- Generate a random number between two figures without decimals.
int number = (int) (Math.random() * (max - min + 1) + min);
There are two changes in this line of code. The first thing we see is the use of (int). Thanks to this, our syntax goes from referring to a decimal variable to an integer one. Notice that the rest of the line is also in parentheses. It starts just before Math.random() and ends at the end. This basically eliminates all decimals. For example, if our random number is 88.87, it will now be 88.
If we continue with the example and want numbers that go from 10 to 100, before we could get figures that ranged from 10.00 to 99.999999… Therefore, if we remove the decimals, what we have is a range between 10 and 99. The problem is that it doesn’t include 100. This leads us to the next change, which is the +1. By adding this element, our problem is solved, since the range now extends to 100.99999…, which when converted to a natural number gives us 100. This way, we now have the range of random numbers we needed, 10-100.
Rounding the Number of Decimals Using the Math.round() Method
In the second topic, I already explained two ways to round the number of decimals and thus avoid a long tail of numbers. As I promised you at that time, here I bring you another way to achieve it. In my opinion, the simplest one, although I considered it important to add the other two in this book. As you’ve already seen in the title of this section, we’re going to use the Math.round() method, the same one we’ve been using throughout this topic to generate random numbers. I’ll explain it to you directly with examples.
Rounding to two decimals:
public class Main {
public static void main(String[] args) {
double number = 3.14159;
double result = Math.round(number * 100.0) / 100.0;
System.out.println("Number with two decimals: " + result); // Output: 3.14
}
}
Rounding to three decimals:
Rounding to three decimals:
public class Main {
public static void main(String[] args) {
double num = Math.random() * 10;
double result = Math.round(num * 1000.0) / 1000.0;
System.out.println("Random number rounded to three decimals: " + result);
}
}
Summary of what you’ve learned
Random numbers are essential in programming in general. They are used, among other things, to simulate random events in games and simulations, generate data for software testing, or optimize algorithms. Additionally, they are crucial in computer security, serving to create cryptographic keys and in data analysis to model statistical distributions. These uses improve the security and performance of computer systems.
In Java, there are several ways to generate random numbers, but we are going to focus on one in particular. We will create them using the Math class and its random() method. This method produces numbers between 0.0 (inclusive) and 1.0 (exclusive). With this numerical range we are somewhat limited, but this is not a problem since with ingenuity we can access other values.
To generate a random number between two decimal numbers, the formula Math.random() * (max – min) + min is used.
To obtain a random integer in a specific range, the result is converted to an integer and the range is adjusted by adding 1, thus guaranteeing the inclusion of the upper value in the range.