How to get a random number between 10 and 20

Alright i am currently making a text based RPG and im having trouble making a random number between like lets say 10 and 20.

I currently know how to make random numbers between 0 and a number.

this is the code i use to get a random number between 0 and 20.

roll = (int)(Math.random() * 20);

what i need is a number between 10 and 20 any help on how to do this would be nice.

I like easy questions :slight_smile:
Random r = new Random();
roll = r.nextInt() % 10 + 10;

so that would make it between 10 and 20 right…

so if i wanted to make it between 20 and 50 would i just do this?
roll = r.nextInt() % 20 + 30;

i tried what you did and it worked for a number between 10 and 20 but then when i tried getting between 20 and 25 i did r.nextInt() % 20 + 5; and i got -8 lol…


Random rand = new Random()

int number = rand.nextInt(10) + 10;  // number from 10 to 19 inclusive
int number = rand.nextInt(11) + 10;  // number from 10 to 20 inclusive
int number = rand.nextInt(6) + 20;  // number from 20 to 25 inclusive

The number inside the nextInt is the range of numbers that you want. The number added after is the bottom of that range.

That gives you 0 to 19 (inclusive).

random.nextInt(20)+1

Gives you 1 to 20 (inclusive). Like a typical 20 sided dice would do.

random.nextInt(biggestPossibleValue+1)+smallestPossibleValue

Gives you smallestPossibleValue to biggestPossibleValue (inclusive, of course).

Thank you for the help. i have 1 other questionthough and i might sound stupid but how do you get all the class files and pictures for a JFrame into an executable jar file?

If you are using Eclipse then easiest way is to Right-Click on your project (in Package Explorer)… Then ‘Export’ --> Java ‘JAR file’ etc…
After which you can select with pictures/classes you want to include.

thank you i will try that

public int rand(int s, int e) {
return (s + (int) (Math.random() * (e-s)));
}

Not quite.

random.nextInt((biggestPossibleValue-smallestPossibleValue)+1)+smallestPossibleValue

Gives you smallestPossibleValue to biggestPossibleValue (inclusive, of course).

Heh. That happened because I was unable to find a nice name for deltaThing… and then I thought… hey, using the biggest possible value would be nicer… but I didn’t pay enough attention. :wink: