Introduction
This article is part of the Scala knowledge bits Series.
Periodically, I will publish new exercises so you can slowly build up knowledge about Scala.
It is designed to be done in a very short amount of time and learn a little bit each day, just to create a routine.
This episode will teach you about Scala Random.
Hope you are going to enjoy it! It is designed for anyone to learn Scala from scratch and slowly learn, one Bit at a time.
After this Bit, I would love to hear your feedback in the comments down below.
Feel free to join the Discord server as well if you would like some help and support from the rest of our community.
What are we learning today?
Today we are going to learn about Scala Random !
A random number generator will generate a number that cannot be predicted.
Although, it is impossible to generate true random number on a computer.
Time to try on the exercise on your own and scroll down for more information when you are done or if you are stuck.
Exercise
Here is an exercise to complete today.
If I did my job well, you should be able to guess by yourself the solution based on what you previously learned and based on the clues.
But if you get stuck, scroll down to get more information.
The goal of the exercise is to replace the ???
by a piece of code so that the exercise compiles and that’s how you win! Good luck!
You can fill the exercise right in here:
Or, if it does not load, go on to Scastie (jVPerO3XQwWD2dyHZeWgww).
More information about Scala Random
In this exercise you will learn (or have learned, if you have already solved the puzzle) about Scala Random.
Please take a look at line 3 with the import
statement. This tells the compiler that, to be able to compile this code, it will need to fetch this other component. In most Scala code you are going to see in your professional life, there will be import
statements at the beginning of the files. But, do not worry, those are usually added automatically by the development environment (IDE) so you don’t have to care for it quite yet.
You recognize the new
from the SKB about class
. It tells us that scala.util.Random
is a class
that needs to be instantiated before able to use it.
Now we have a random generator, what can we do with it? You can generate a lot of different types, here we are only focusing on Int
to simplify things but you can take a look at scala.util.Random documentation to see what else is available.
Try running the code several times, do you see that the number generated are different each time?
However, the number generated by the generator started with a seed always generate the same series of number. This is because there are no true random in a computer. A random generator is a function that given a number generate a new number. The starting number is the seed
. If you are playing procedurally generated games, such as Minecraft for instance, this is what the seed
is for, it initializes the random generator.
One interesting part of this SKB is the randomInt
method. Did you figure out what was the missing part ? If not, here is the solution:
rand.nextInt(max - min) + min
The first part ( rand.nextInt(max - min)
) will return an Integer between 0
and max - min
but we want something between min
and max
. We need to add min
. That way, we generate a number between 0 + min
and max - min + min
, which resolve to min
to max
.
There is a little brain candy at the end of the code, did you notice it ? for
. This is called a for-comprehension. We are going to go more into details about it in up-coming SKBs.
An other brain candy is the range
. In Scala, you can describe a range of number in different ways:
0 to 2
will generate the numbers0, 1, 2
0 until 2
which will generate the numbers0, 1
0 until 10 by 3
which will generate the numbers0, 3, 6, 9
Feel free to go back to the exercise, modify the code to try out new things and get a better intuition for Scala Random.
Conclusion
I hope you have learned something new or had fun during this Scala Knowledge Bit.
Please ask questions or post feedback in the comments below.
Feel free to try on the next Scala Knowledege Bit.
If you are curious about the previous Scala knowledge Bits, go check it out! 🙂