Tuesday, October 19, 2021

Generating a Random Number in Java

Here's how we generate a random number in Java. It's quite simple. First we import java.util.Random. Then we call that class and grab the next int with an upper bound of 25. That is to say we pick a number from zero to twenty-five. It cannot go below zero it cannot go above twenty-five.


import java.util.Random;

public class RandomNumber {

public static void main(String[] args) {
Random rand = new Random();
int int_random = rand.nextInt(25);

System.out.println(int_random);
}

}

No comments:

Post a Comment

Life Hurts

 Let's face it, life hurts. I'm not sure how else to put it. There are times in your life that you wish didn't hurt, but they co...