Random number generation in Java

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

random-number-generation_vertex-academy

Random number generation is an important and necessary subject. Indeed, it will come in handy for you on countless occasions.

For example:

  • when completing a group of random numbers;
  • when shuffling a pack of cards;
  • when selecting a random element of a sequence;
  • and so on.

There are several ways to generate a random number. In this article we will cover the generation by means of Math.random().

There is the package java.lang in the Java class library, which has the Math class, and the Math class in its turn has the random() method. Please see the picture below.

class-math_vertex-academy

So, a random number is generated every time you call Math.random() by means of the special algorithm (on the particular instruction). Is it possible to predict which number will be generated? In theory it is possible, but it’s really hard to do. And since there is a small chance to predict which number will be generated, such numbers are called pseudorandom instead of just random.

You need to know three things:

1.By default Math.random() generates real numbers from the interval [0;1), i.e. from 0 inclusive up to 1 exclusive.

“Up to 1 exclusive” means that Math.random cannot generate the number 1. But it can generate for example 0,999 – i.e. any number less than 1. Try to run this code on your computer and you will see, that the console displays the real numbers from 0 to any number less than 1.

So if you need to generate a random number from the interval [0;1) – you already know how to do it.

2.And what if we need to generate a number from another interval, which is different from [0;1)?

It’s easy! Let’s formally write our range as [a;b). That is, the lower limit is a, and the upper limit is b. Then, in order to generate a real number within the preset range we need the write the following:

( Math.random() * (b-a) ) + a

Let’s look at the examples below.

Example No.1

So, in order to get a random real number within the range [0;3) (3 exclusively), we use the following:

How does it work?

The range [0;1) is multiplied by 3. Accordingly,

Lower limit: 0*3=0

Upper limit: 1+3=3

We get the range [0;3)

If we apply out formula, we get the following:

( Math.random() * (3 - 0) ) + 0  =>  Math.random() * 3

Which is displayed in the program.

Example No.2

Let us assume, that we need to get a floating point number within the range [2;3) (3 exclusive).

How does it work?

We add 2 to the interval [0;1). Accordingly,

Lower limit: 0+2=2

Upper limit: 1+2 =3

We get the interval [2;3)

Under the formula we get the following:

( Math.random() * (3 - 2) ) + 2  =>  Math.random() * 1 + 2  =>  Math.random() + 2

Example No.3

Let’s say we need to get a real number in the range [20;60) (60 exclusive).

How does it work?

Step No.1: The interval [0;1) is multiplied by 40. Accordingly,

Lower limit: 0*40=0

Upper limit: 1*40=40

We get the following interval: [0;40)

Step No.1: We add 20 to the interval [0;40). Accordingly,

Lower limit: 0+20=20

Upper limit: 40+20=60

We get the following interval: [20;60)

If we just apply it to our formula, we get:

( Math.random() * (60 - 20) ) + 20  =>  Math.random() * 40 + 20

Example No.4

We need a real number in the range [ -100; +100) (100 exclusive).

According to the formula:

( Math.random() * (100 - (-100)) ) + (-100)  =>  Math.random() * 200 - 100

3.And how do I generate an integer value?

Example No.5

Let’s imagine that we need to generate a number in the range [0;2]. Please note that after the number 2 we used the square bracket instead of round bracket. It means, that we want the range to include the number 2. How do we do it? Like this:

How does it work:

Step No.1: The interval [0;1) is multiplied by 3. Accordingly,

Lower limit: 0*3=0

Upper limit: 1*3=3

We got the range [0;3). The bracket after the number 3 is round. That means that the number 3 is not included in the range, i.e. 2,999 is the maximum number which can be generated in this range. In short – we are dealing with real (fraction) numbers.

Step No.2: That’s why we apply (int) before Math.random()*3. The fractional part is cut off and we get the range [0;2].

If we check our formula, it has only small differences.

In case if the real number and upper limit include b – [a;b] :

( Math.random() * (b - a + 1) + a

And if it includes only the integer numbers:

(int)(( Math.random() * (b - a + 1) + a)

Example No.6

We need to generate a number within the interval [3;4]. Please note, that we used the square bracket after the number 4. It means, that we want the range to include the number 4. How do we do it? Like this:

How does it work?

Step No.1: We multiply the range [0;1) by 2. Accordingly,

Lower limit: 0*2=0

Upper limit: 1*2=2

We got the range [0;2). The bracket after the number 2 is round. That means that the number 2 is not included in the range, i.e. 1,999 is the maximum number which can be generated in this range. And it means, that we are dealing with a real (fraction) number.

Step No.2: That’s why we apply (int) before Math.random()*3. The fractional part is cut off and we get the range [0;1]. Please note – now the range includes the number 1.

Step No.3: We add 3 to the interval [0;1]. Accordingly,

Lower limit: 0+3=3

Upper limit: 1+3=4

We got the new range [3;4].

Now we apply the formula:

(int)(Math.random() * (4 - 3 + 1) + 3)  =>   (int)(Math.random() *2 + 3)

Example No.7

We need an integer value within the range [-100; +100] (100 inclusive).

Example No.8

We need an integer value within the range [-200; 400] (400 inclusive).

Example No.9

We need an integer value within the range [1;2] (2 inclusive).

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 5 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

Самоучители--узнать детальнее--