StackIsLife

How Do I Generate Random Numbers In Java?

September 26, 2019

For example, create a new object and give it a seed:

Random random = new Random (1L);

Seeds are important because computers are actually pretty bad at picking random numbers. Let’s walk through the theoretical design of a random number program. Programs follow strict instructions, and so if one made a program to pick random numbers, he/she might say that the first random number you return for your random object will be:

3*2*4%2/9+10/29 = 0.3448275862

Then the next random number would have to have a different equation (if you used the equation that produced your first random number, then you’d just get the same number every time).

Let’s instead use the result of the first operation to be input of the next number.

0.3448275862*34*4+2+90=138.896551723

Great, but you can’t keep on defining these, it won’t create enough random numbers. Furthermore, every time you create a new version of your object, it would start all the way from the first number and you’ll get that every single time. One would get the same sequence of random numbers every time. Doesn’t seem too random, does it? Our first line of code will keep outputting the same numbers over and over again.

These numbers are examples of pseudorandom numbers, as opposed to random numbers. True random numbers cannot be predicted with any accuracy. For example, the heartbeat of a person is random. While one’s heart may beat regularly, when an exact beat will take place cannot exactly be predicted. If a person is watching TV and an action scene comes up, their heart rate may spike. Even being exposed to bright light drinking coffee changes the rate of your heart. These irregularities are unpredictable, and can be used to create a random number. For example your bpm can be 70, 90, 100,80, 73, etc.

When you create a new random object in java, it’ll automatically seed your object with the current time in nanoseconds. This makes it so you’ll get different values for each of your random objects. It won’t be completely random as it’s easily deterministic, but it’s good enough for most applications.

So the final code looks like this:

Random random = new Random();
System.out.println(random.nextInt());

If you want to be within a range, you can specify a min and max, or you can perform a mod operation with the highest value you want. The min and max is the technically correct way of doing it, but if you’re feeling lazy and just writing a test, then the mod way is less thinking and it still gets the job done.

Min max:

Random random = new Random();
System.out.println((random.nextInt(max) - min + 1));

Modulus:

Random random = new Random();
System.out.println((random.nextInt() % 999999));

Helpful link - The java.util.Random library: https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

Link that inspired the post: How Do I Generate Random Integers Within A Specific Range In Java


Written by Scott Hansen who works in New York City building great stuff.