# how to use nextInt() to generate random integers?

• July 22nd, 2011, 11:10
rph
how to use nextInt() to generate random integers?
So basically for my assignment, I need to generate a random integer number between 20 and 90. My textbook leaves a lot to be desired in the explanation of using nextInt(). I want to assign the random integer value to an int that I have already declared called randomAngle. Can anyone explain to me the correct syntax to get this?
I hope I've made myself clear, many thanks :)
• July 22nd, 2011, 11:48
Norm
Re: how to use nextInt() to generate random integers?
Quote:

using nextInt()
What class is the nextInt() method in?
The API doc for the class and that method should explain what it does.
Link to the API doc:Java Platform SE 6
• July 22nd, 2011, 12:34
rph
Re: how to use nextInt() to generate random integers?
Figured it out now, all i had to do was write:

static Random randGen = new Random();
int randomAng = randGen.nextInt(70) + 20;

in the public class and make sure I put:

import java.util.Random;

at the beginning.
Many thanks for your help :)
• July 24th, 2011, 20:49
Junky
Re: how to use nextInt() to generate random integers?
Code java:

```static Random randGen = new Random(); int randomAng = randGen.nextInt(70) + 20;```
Your code will generate random numbers from 20 to 89 inclusive. Read the API to find out why 90 will be excluded.
• November 17th, 2012, 09:46
jaehoju
Re: how to use nextInt() to generate random integers?
why we need static?
• January 3rd, 2013, 00:58