0

So I am trying to generate a random number, but I can't use the Java random function because I need the numbers to be in the range of 1-25. What is the easiest aka most efficient way of doing this? If possible, an explanation would be great!

tantantheman
  • 53
  • 2
  • 8

3 Answers3

3
int random = (int)(Math.random() * 25 + 1);

or

int random = new Random.nextInt(24) + 1;
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

I prefer to use the Java Random Class.

import java.util.Random;

And then generate the random nuber like this - assuming you want an integer:

Random gen = new Random();
int r = gen.nextInt(25) + 1;
finn
  • 1,155
  • 8
  • 9
0

Random numbers between 1 and 25 (1-25)

Random.nextInt(24) + 1
user2693979
  • 2,482
  • 4
  • 24
  • 23